Skip to content

Add some "could be written as" examples #4436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions clippy_lints/src/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
21 changes: 20 additions & 1 deletion clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)`"
Expand Down Expand Up @@ -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()`"
Expand All @@ -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,
Expand Down Expand Up @@ -889,6 +904,10 @@ declare_clippy_lint! {
/// ```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"
Expand Down
6 changes: 6 additions & 0 deletions clippy_lints/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ declare_clippy_lint! {
/// # 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()`"
Expand Down
4 changes: 4 additions & 0 deletions clippy_lints/src/needless_bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }`"
Expand Down
13 changes: 13 additions & 0 deletions clippy_lints/src/ranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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`"
Expand All @@ -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`"
Expand Down
6 changes: 6 additions & 0 deletions clippy_lints/src/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ declare_clippy_lint! {
/// 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"
Expand Down