Add examples to some complexity lints

This commit is contained in:
BO41 2019-08-20 16:55:17 +02:00
parent 3f56cb5d8b
commit eebb7cd839
4 changed files with 41 additions and 1 deletions

View File

@ -312,6 +312,14 @@ declare_clippy_lint! {
/// 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 @@ declare_clippy_lint! {
/// # 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 @@ declare_clippy_lint! {
/// # 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 @@ declare_clippy_lint! {
/// **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,

View File

@ -31,6 +31,10 @@ declare_clippy_lint! {
/// 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 @@ declare_clippy_lint! {
/// # 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 @@ declare_clippy_lint! {
/// ```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 @@ declare_clippy_lint! {
/// ```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`"