Auto merge of #4436 - BO41:written_as, r=phansch

Add some "could be written as" examples

fixes #4405
changelog: none
This commit is contained in:
bors 2019-08-26 11:11:57 +00:00
commit ba6681300e
6 changed files with 57 additions and 1 deletions

View File

@ -312,6 +312,14 @@
/// for i in 0..v.len() { foo(v[i]); }
/// for i in 0..v.len() { bar(i, v[i]); }
/// ```
/// Could be written as
/// ```rust
/// # let v = vec![1];
/// # fn foo(bar: usize) {}
/// # fn bar(bar: usize, baz: usize) {}
/// for item in &v { foo(*item); }
/// for (i, item) in v.iter().enumerate() { bar(i, *item); }
/// ```
pub EXPLICIT_COUNTER_LOOP,
complexity,
"for-looping with an explicit counter when `_.enumerate()` would do"

View File

@ -302,6 +302,11 @@
/// # let vec = vec![1];
/// vec.iter().filter(|x| **x == 0).next();
/// ```
/// Could be written as
/// ```rust
/// # let vec = vec![1];
/// vec.iter().find(|x| **x == 0);
/// ```
pub FILTER_NEXT,
complexity,
"using `filter(p).next()`, which is more succinctly expressed as `.find(p)`"
@ -425,6 +430,11 @@
/// # let vec = vec![1];
/// vec.iter().find(|x| **x == 0).is_some();
/// ```
/// Could be written as
/// ```rust
/// # let vec = vec![1];
/// vec.iter().any(|x| *x == 0);
/// ```
pub SEARCH_IS_SOME,
complexity,
"using an iterator search followed by `is_some()`, which is more succinctly expressed as a call to `any()`"
@ -442,7 +452,12 @@
/// **Example:**
/// ```rust
/// let name = "foo";
/// name.chars().next() == Some('_');
/// if name.chars().next() == Some('_') {};
/// ```
/// Could be written as
/// ```rust
/// let name = "foo";
/// if name.starts_with('_') {};
/// ```
pub CHARS_NEXT_CMP,
complexity,
@ -889,6 +904,10 @@
/// ```rust
/// let _ = [1, 2, 3].into_iter().map(|x| *x).collect::<Vec<u32>>();
/// ```
/// Could be written as:
/// ```rust
/// let _ = [1, 2, 3].iter().map(|x| *x).collect::<Vec<u32>>();
/// ```
pub INTO_ITER_ON_ARRAY,
correctness,
"using `.into_iter()` on an array"

View File

@ -105,6 +105,12 @@
/// # let y = String::from("foo");
/// if x.to_owned() == y {}
/// ```
/// Could be written as
/// ```rust
/// # let x = "foo";
/// # let y = String::from("foo");
/// if x == y {}
/// ```
pub CMP_OWNED,
perf,
"creating owned instances for comparing with others, e.g., `x == \"foo\".to_string()`"

View File

@ -31,6 +31,10 @@
/// true
/// }
/// ```
/// Could be written as
/// ```rust,ignore
/// !x
/// ```
pub NEEDLESS_BOOL,
complexity,
"if-statements with plain booleans in the then- and else-clause, e.g., `if p { true } else { false }`"

View File

@ -43,6 +43,11 @@
/// # let x = vec![1];
/// x.iter().zip(0..x.len());
/// ```
/// Could be written as
/// ```rust
/// # let x = vec![1];
/// x.iter().enumerate();
/// ```
pub RANGE_ZIP_WITH_LEN,
complexity,
"zipping iterator with a range when `enumerate()` would do"
@ -64,6 +69,10 @@
/// ```rust,ignore
/// for x..(y+1) { .. }
/// ```
/// Could be written as
/// ```rust,ignore
/// for x..=y { .. }
/// ```
pub RANGE_PLUS_ONE,
complexity,
"`x..(y+1)` reads better as `x..=y`"
@ -82,6 +91,10 @@
/// ```rust,ignore
/// for x..=(y-1) { .. }
/// ```
/// Could be written as
/// ```rust,ignore
/// for x..y { .. }
/// ```
pub RANGE_MINUS_ONE,
complexity,
"`x..=(y-1)` reads better as `x..y`"

View File

@ -52,6 +52,12 @@
/// a = b;
/// b = a;
/// ```
/// Could be written as:
/// ```rust
/// # let mut a = 1;
/// # let mut b = 2;
/// std::mem::swap(&mut a, &mut b);
/// ```
pub ALMOST_SWAPPED,
correctness,
"`foo = bar; bar = foo` sequence"