Skip to content

Commit 288809c

Browse files
committed
Auto merge of #23682 - tamird:DRY-is-empty, r=alexcrichton
r? @alexcrichton
2 parents e40449e + c55ae1d commit 288809c

File tree

104 files changed

+262
-259
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+262
-259
lines changed

src/compiletest/compiletest.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ pub fn make_metrics_test_closure(config: &Config, testfile: &Path) -> test::Test
368368
fn extract_gdb_version(full_version_line: Option<String>) -> Option<String> {
369369
match full_version_line {
370370
Some(ref full_version_line)
371-
if full_version_line.trim().len() > 0 => {
371+
if !full_version_line.trim().is_empty() => {
372372
let full_version_line = full_version_line.trim();
373373

374374
// used to be a regex "(^|[^0-9])([0-9]\.[0-9])([^0-9]|$)"
@@ -408,7 +408,7 @@ fn extract_lldb_version(full_version_line: Option<String>) -> Option<String> {
408408

409409
match full_version_line {
410410
Some(ref full_version_line)
411-
if full_version_line.trim().len() > 0 => {
411+
if !full_version_line.trim().is_empty() => {
412412
let full_version_line = full_version_line.trim();
413413

414414
for (pos, l) in full_version_line.char_indices() {
@@ -426,7 +426,7 @@ fn extract_lldb_version(full_version_line: Option<String>) -> Option<String> {
426426
let vers = full_version_line[pos + 5..].chars().take_while(|c| {
427427
c.is_digit(10)
428428
}).collect::<String>();
429-
if vers.len() > 0 { return Some(vers) }
429+
if !vers.is_empty() { return Some(vers) }
430430
}
431431
println!("Could not extract LLDB version from line '{}'",
432432
full_version_line);

src/compiletest/runtest.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ fn check_debugger_output(debugger_run_result: &ProcRes, check_lines: &[String])
864864
}
865865
first = false;
866866
}
867-
if !failed && rest.len() == 0 {
867+
if !failed && rest.is_empty() {
868868
i += 1;
869869
}
870870
if i == num_check_lines {
@@ -1662,7 +1662,7 @@ fn _arm_push_aux_shared_library(config: &Config, testfile: &Path) {
16621662
// codegen tests (vs. clang)
16631663

16641664
fn append_suffix_to_stem(p: &Path, suffix: &str) -> PathBuf {
1665-
if suffix.len() == 0 {
1665+
if suffix.is_empty() {
16661666
p.to_path_buf()
16671667
} else {
16681668
let mut stem = p.file_stem().unwrap().to_os_string();

src/doc/reference.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3788,7 +3788,7 @@ its type parameters are types:
37883788

37893789
```ignore
37903790
fn map<A: Clone, B: Clone>(f: |A| -> B, xs: &[A]) -> Vec<B> {
3791-
if xs.len() == 0 {
3791+
if xs.is_empty() {
37923792
return vec![];
37933793
}
37943794
let first: B = f(xs[0].clone());

src/libcollections/btree/map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,7 @@ mod stack {
692692
// We've reached the root, so no matter what, we're done. We manually
693693
// access the root via the tree itself to avoid creating any dangling
694694
// pointers.
695-
if self.map.root.len() == 0 && !self.map.root.is_leaf() {
695+
if self.map.root.is_empty() && !self.map.root.is_leaf() {
696696
// We've emptied out the root, so make its only child the new root.
697697
// If it's a leaf, we just let it become empty.
698698
self.map.depth -= 1;

src/libcollections/btree/node.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,9 @@ impl <K, V> Node<K, V> {
585585
self._len
586586
}
587587

588+
/// Does the node not contain any key-value pairs
589+
pub fn is_empty(&self) -> bool { self.len() == 0 }
590+
588591
/// How many key-value pairs the node can fit
589592
pub fn capacity(&self) -> usize {
590593
self._capacity
@@ -1097,7 +1100,7 @@ impl<K, V> Node<K, V> {
10971100
/// When a node has no keys or values and only a single edge, extract that edge.
10981101
pub fn hoist_lone_child(&mut self) {
10991102
// Necessary for correctness, but in a private module
1100-
debug_assert!(self.len() == 0);
1103+
debug_assert!(self.is_empty());
11011104
debug_assert!(!self.is_leaf());
11021105

11031106
unsafe {
@@ -1225,7 +1228,7 @@ impl<K, V> Node<K, V> {
12251228
/// because we have one too many, and our parent now has one too few
12261229
fn split(&mut self) -> (K, V, Node<K, V>) {
12271230
// Necessary for correctness, but in a private function
1228-
debug_assert!(self.len() > 0);
1231+
debug_assert!(!self.is_empty());
12291232

12301233
let mut right = if self.is_leaf() {
12311234
Node::new_leaf(self.capacity())

src/libcore/char.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl CharExt for char {
227227
#[inline]
228228
pub fn encode_utf8_raw(code: u32, dst: &mut [u8]) -> Option<usize> {
229229
// Marked #[inline] to allow llvm optimizing it away
230-
if code < MAX_ONE_B && dst.len() >= 1 {
230+
if code < MAX_ONE_B && !dst.is_empty() {
231231
dst[0] = code as u8;
232232
Some(1)
233233
} else if code < MAX_TWO_B && dst.len() >= 2 {
@@ -258,7 +258,7 @@ pub fn encode_utf8_raw(code: u32, dst: &mut [u8]) -> Option<usize> {
258258
#[inline]
259259
pub fn encode_utf16_raw(mut ch: u32, dst: &mut [u16]) -> Option<usize> {
260260
// Marked #[inline] to allow llvm optimizing it away
261-
if (ch & 0xFFFF) == ch && dst.len() >= 1 {
261+
if (ch & 0xFFFF) == ch && !dst.is_empty() {
262262
// The BMP falls through (assuming non-surrogate, as it should)
263263
dst[0] = ch as u16;
264264
Some(1)

src/libcore/slice.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl<T> SliceExt for [T] {
204204

205205
#[inline]
206206
fn first(&self) -> Option<&T> {
207-
if self.len() == 0 { None } else { Some(&self[0]) }
207+
if self.is_empty() { None } else { Some(&self[0]) }
208208
}
209209

210210
#[inline]
@@ -217,7 +217,7 @@ impl<T> SliceExt for [T] {
217217

218218
#[inline]
219219
fn last(&self) -> Option<&T> {
220-
if self.len() == 0 { None } else { Some(&self[self.len() - 1]) }
220+
if self.is_empty() { None } else { Some(&self[self.len() - 1]) }
221221
}
222222

223223
#[inline]
@@ -296,7 +296,7 @@ impl<T> SliceExt for [T] {
296296

297297
#[inline]
298298
fn first_mut(&mut self) -> Option<&mut T> {
299-
if self.len() == 0 { None } else { Some(&mut self[0]) }
299+
if self.is_empty() { None } else { Some(&mut self[0]) }
300300
}
301301

302302
#[inline]
@@ -1306,7 +1306,7 @@ impl<'a, T> Iterator for Chunks<'a, T> {
13061306

13071307
#[inline]
13081308
fn next(&mut self) -> Option<&'a [T]> {
1309-
if self.v.len() == 0 {
1309+
if self.v.is_empty() {
13101310
None
13111311
} else {
13121312
let chunksz = cmp::min(self.v.len(), self.size);
@@ -1318,7 +1318,7 @@ impl<'a, T> Iterator for Chunks<'a, T> {
13181318

13191319
#[inline]
13201320
fn size_hint(&self) -> (usize, Option<usize>) {
1321-
if self.v.len() == 0 {
1321+
if self.v.is_empty() {
13221322
(0, Some(0))
13231323
} else {
13241324
let n = self.v.len() / self.size;
@@ -1333,7 +1333,7 @@ impl<'a, T> Iterator for Chunks<'a, T> {
13331333
impl<'a, T> DoubleEndedIterator for Chunks<'a, T> {
13341334
#[inline]
13351335
fn next_back(&mut self) -> Option<&'a [T]> {
1336-
if self.v.len() == 0 {
1336+
if self.v.is_empty() {
13371337
None
13381338
} else {
13391339
let remainder = self.v.len() % self.size;
@@ -1384,7 +1384,7 @@ impl<'a, T> Iterator for ChunksMut<'a, T> {
13841384

13851385
#[inline]
13861386
fn next(&mut self) -> Option<&'a mut [T]> {
1387-
if self.v.len() == 0 {
1387+
if self.v.is_empty() {
13881388
None
13891389
} else {
13901390
let sz = cmp::min(self.v.len(), self.chunk_size);
@@ -1397,7 +1397,7 @@ impl<'a, T> Iterator for ChunksMut<'a, T> {
13971397

13981398
#[inline]
13991399
fn size_hint(&self) -> (usize, Option<usize>) {
1400-
if self.v.len() == 0 {
1400+
if self.v.is_empty() {
14011401
(0, Some(0))
14021402
} else {
14031403
let n = self.v.len() / self.chunk_size;
@@ -1412,7 +1412,7 @@ impl<'a, T> Iterator for ChunksMut<'a, T> {
14121412
impl<'a, T> DoubleEndedIterator for ChunksMut<'a, T> {
14131413
#[inline]
14141414
fn next_back(&mut self) -> Option<&'a mut [T]> {
1415-
if self.v.len() == 0 {
1415+
if self.v.is_empty() {
14161416
None
14171417
} else {
14181418
let remainder = self.v.len() % self.chunk_size;

src/libcore/str/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ enum OldSearcher {
11191119
impl OldSearcher {
11201120
#[allow(dead_code)]
11211121
fn new(haystack: &[u8], needle: &[u8]) -> OldSearcher {
1122-
if needle.len() == 0 {
1122+
if needle.is_empty() {
11231123
// Handle specially
11241124
unimplemented!()
11251125
// FIXME: Tune this.

src/libcore/str/pattern.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ fn str_search_step<F, G>(mut m: &mut StrSearcher,
457457
{
458458
if m.state.done() {
459459
SearchStep::Done
460-
} else if m.needle.len() == 0 && m.start <= m.end {
460+
} else if m.needle.is_empty() && m.start <= m.end {
461461
// Case for needle == ""
462462
if let State::Reject(a, b) = m.state.take() {
463463
SearchStep::Reject(a, b)

src/libfmt_macros/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ impl<'a> Parser<'a> {
371371
None => {
372372
let tmp = self.cur.clone();
373373
match self.word() {
374-
word if word.len() > 0 => {
374+
word if !word.is_empty() => {
375375
if self.consume('$') {
376376
CountIsName(word)
377377
} else {
@@ -463,7 +463,7 @@ mod tests {
463463
fn musterr(s: &str) {
464464
let mut p = Parser::new(s);
465465
p.next();
466-
assert!(p.errors.len() != 0);
466+
assert!(!p.errors.is_empty());
467467
}
468468

469469
#[test]

src/libgetopts/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ fn format_option(opt: &OptGroup) -> String {
804804
}
805805

806806
// Use short_name is possible, but fallback to long_name.
807-
if opt.short_name.len() > 0 {
807+
if !opt.short_name.is_empty() {
808808
line.push('-');
809809
line.push_str(&opt.short_name[..]);
810810
} else {

src/liblog/directive.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub fn parse_logging_spec(spec: &str) -> (Vec<LogDirective>, Option<String>) {
4545
return (dirs, None);
4646
}
4747
mods.map(|m| { for s in m.split(',') {
48-
if s.len() == 0 { continue }
48+
if s.is_empty() { continue }
4949
let mut parts = s.split('=');
5050
let (log_level, name) = match (parts.next(), parts.next().map(|s| s.trim()), parts.next()) {
5151
(Some(part0), None, None) => {

src/librustc/metadata/creader.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub fn validate_crate_name(sess: Option<&Session>, s: &str, sp: Option<Span>) {
8080
(None, Some(sess)) => sess.err(s),
8181
}
8282
};
83-
if s.len() == 0 {
83+
if s.is_empty() {
8484
say("crate name must not be empty");
8585
}
8686
for c in s.chars() {

src/librustc/metadata/decoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ pub fn get_enum_variants<'tcx>(intr: Rc<IdentInterner>, cdata: Cmd, id: ast::Nod
767767
get_type(cdata, field_ty.id.node, tcx).ty
768768
})
769769
.collect();
770-
let arg_names = if arg_names.len() == 0 { None } else { Some(arg_names) };
770+
let arg_names = if arg_names.is_empty() { None } else { Some(arg_names) };
771771

772772
(None, arg_tys, arg_names)
773773
}
@@ -1383,7 +1383,7 @@ pub fn get_dylib_dependency_formats(cdata: Cmd)
13831383

13841384
debug!("found dylib deps: {}", formats.as_str_slice());
13851385
for spec in formats.as_str_slice().split(',') {
1386-
if spec.len() == 0 { continue }
1386+
if spec.is_empty() { continue }
13871387
let cnum = spec.split(':').nth(0).unwrap();
13881388
let link = spec.split(':').nth(1).unwrap();
13891389
let cnum: ast::CrateNum = cnum.parse().unwrap();

src/librustc/metadata/encoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ fn each_auxiliary_node_id<F>(item: &ast::Item, callback: F) -> bool where
469469
ast::ItemStruct(ref struct_def, _) => {
470470
// If this is a newtype struct, return the constructor.
471471
match struct_def.ctor_id {
472-
Some(ctor_id) if struct_def.fields.len() > 0 &&
472+
Some(ctor_id) if !struct_def.fields.is_empty() &&
473473
struct_def.fields[0].node.kind.is_unnamed() => {
474474
continue_ = callback(ctor_id);
475475
}
@@ -1751,7 +1751,7 @@ fn encode_codemap(ecx: &EncodeContext, rbml_w: &mut Encoder) {
17511751

17521752
for filemap in &codemap.files.borrow()[..] {
17531753

1754-
if filemap.lines.borrow().len() == 0 || filemap.is_imported() {
1754+
if filemap.lines.borrow().is_empty() || filemap.is_imported() {
17551755
// No need to export empty filemaps, as they can't contain spans
17561756
// that need translation.
17571757
// Also no need to re-export imported filemaps, as any downstream

src/librustc/metadata/loader.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,13 @@ impl<'a> Context<'a> {
307307
}
308308

309309
pub fn report_load_errs(&mut self) {
310-
let message = if self.rejected_via_hash.len() > 0 {
310+
let message = if !self.rejected_via_hash.is_empty() {
311311
format!("found possibly newer version of crate `{}`",
312312
self.ident)
313-
} else if self.rejected_via_triple.len() > 0 {
313+
} else if !self.rejected_via_triple.is_empty() {
314314
format!("couldn't find crate `{}` with expected target triple {}",
315315
self.ident, self.triple)
316-
} else if self.rejected_via_kind.len() > 0 {
316+
} else if !self.rejected_via_kind.is_empty() {
317317
format!("found staticlib `{}` instead of rlib or dylib", self.ident)
318318
} else {
319319
format!("can't find crate for `{}`", self.ident)
@@ -325,15 +325,15 @@ impl<'a> Context<'a> {
325325
};
326326
self.sess.span_err(self.span, &message[..]);
327327

328-
if self.rejected_via_triple.len() > 0 {
328+
if !self.rejected_via_triple.is_empty() {
329329
let mismatches = self.rejected_via_triple.iter();
330330
for (i, &CrateMismatch{ ref path, ref got }) in mismatches.enumerate() {
331331
self.sess.fileline_note(self.span,
332332
&format!("crate `{}`, path #{}, triple {}: {}",
333333
self.ident, i+1, got, path.display()));
334334
}
335335
}
336-
if self.rejected_via_hash.len() > 0 {
336+
if !self.rejected_via_hash.is_empty() {
337337
self.sess.span_note(self.span, "perhaps this crate needs \
338338
to be recompiled?");
339339
let mismatches = self.rejected_via_hash.iter();
@@ -353,7 +353,7 @@ impl<'a> Context<'a> {
353353
}
354354
}
355355
}
356-
if self.rejected_via_kind.len() > 0 {
356+
if !self.rejected_via_kind.is_empty() {
357357
self.sess.fileline_help(self.span, "please recompile this crate using \
358358
--crate-type lib");
359359
let mismatches = self.rejected_via_kind.iter();
@@ -517,7 +517,7 @@ impl<'a> Context<'a> {
517517
// library's metadata sections. In theory we should
518518
// read both, but reading dylib metadata is quite
519519
// slow.
520-
if m.len() == 0 {
520+
if m.is_empty() {
521521
return None
522522
} else if m.len() == 1 {
523523
return Some(m.into_iter().next().unwrap())

src/librustc/middle/check_match.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ fn check_for_bindings_named_the_same_as_variants(cx: &MatchCheckCtxt, pat: &Pat)
239239
if let Some(DefLocal(_)) = def {
240240
if ty::enum_variants(cx.tcx, def_id).iter().any(|variant|
241241
token::get_name(variant.name) == token::get_name(ident.node.name)
242-
&& variant.args.len() == 0
242+
&& variant.args.is_empty()
243243
) {
244244
span_warn!(cx.tcx.sess, p.span, E0170,
245245
"pattern binding `{}` is named the same as one \
@@ -636,19 +636,19 @@ fn is_useful(cx: &MatchCheckCtxt,
636636
-> Usefulness {
637637
let &Matrix(ref rows) = matrix;
638638
debug!("{:?}", matrix);
639-
if rows.len() == 0 {
639+
if rows.is_empty() {
640640
return match witness {
641641
ConstructWitness => UsefulWithWitness(vec!()),
642642
LeaveOutWitness => Useful
643643
};
644644
}
645-
if rows[0].len() == 0 {
645+
if rows[0].is_empty() {
646646
return NotUseful;
647647
}
648648
assert!(rows.iter().all(|r| r.len() == v.len()));
649649
let real_pat = match rows.iter().find(|r| (*r)[0].id != DUMMY_NODE_ID) {
650650
Some(r) => raw_pat(r[0]),
651-
None if v.len() == 0 => return NotUseful,
651+
None if v.is_empty() => return NotUseful,
652652
None => v[0]
653653
};
654654
let left_ty = if real_pat.id == DUMMY_NODE_ID {

src/librustc/middle/dead.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
182182

183183
fn mark_live_symbols(&mut self) {
184184
let mut scanned = HashSet::new();
185-
while self.worklist.len() > 0 {
185+
while !self.worklist.is_empty() {
186186
let id = self.worklist.pop().unwrap();
187187
if scanned.contains(&id) {
188188
continue

0 commit comments

Comments
 (0)