Skip to content

Commit

Permalink
Cleanup for ref_mut_iter_method_chain
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarcho committed Nov 12, 2021
1 parent 6cbfa45 commit 96c400b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
4 changes: 1 addition & 3 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1867,19 +1867,17 @@ declare_clippy_lint! {
/// Check for `&mut iter` followed by a method call.
///
/// ### Why is this bad?
/// This requires using parenthesis to signify precedence.
/// Using `(&mut iter)._` requires using parenthesis to signify precedence.
///
/// ### Example
/// ```rust
/// let mut iter = ['a', 'b', '.', 'd'].iter();
/// let before_dot = (&mut iter).take_while(|&&c| c != '.').collect::<Vec<_>>();
/// let after_dot = iter.collect::<Vec<_>>();
/// ```
/// Use instead:
/// ```rust
/// let mut iter = ['a', 'b', '.', 'd'].iter();
/// let before_dot = iter.by_ref().take_while(|&&c| c != '.').collect::<Vec<_>>();
/// let after_dot = iter.collect::<Vec<_>>();
/// ```
pub REF_MUT_ITER_METHOD_CHAIN,
style,
Expand Down
6 changes: 5 additions & 1 deletion tests/ui/ref_mut_iter_method_chain.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ fn main() {

// Don't lint. No method chain.
for &x in &mut iter {
print!("{}", x)
print!("{}", x);
}

for &x in iter.by_ref().filter(|&&x| x % 2 == 0) {
print!("{}", x);
}

let iter = &mut iter;
Expand Down
6 changes: 5 additions & 1 deletion tests/ui/ref_mut_iter_method_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ fn main() {

// Don't lint. No method chain.
for &x in &mut iter {
print!("{}", x)
print!("{}", x);
}

for &x in (&mut iter).filter(|&&x| x % 2 == 0) {
print!("{}", x);
}

let iter = &mut iter;
Expand Down
12 changes: 9 additions & 3 deletions tests/ui/ref_mut_iter_method_chain.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,22 @@ LL | let _ = (&mut m!(iter)).find(|&&x| x == 1);
| ^^^^^^^^^^^^^^^ help: try: `m!(iter).by_ref()`

error: use of `&mut` on an iterator in a method chain
--> $DIR/ref_mut_iter_method_chain.rs:33:5
--> $DIR/ref_mut_iter_method_chain.rs:32:15
|
LL | for &x in (&mut iter).filter(|&&x| x % 2 == 0) {
| ^^^^^^^^^^^ help: try: `iter.by_ref()`

error: use of `&mut` on an iterator in a method chain
--> $DIR/ref_mut_iter_method_chain.rs:37:5
|
LL | (&mut *iter).find(|&&x| x == 1);
| ^^^^^^^^^^^^ help: try: `iter.by_ref()`

error: use of `&mut` on an iterator in a method chain
--> $DIR/ref_mut_iter_method_chain.rs:36:5
--> $DIR/ref_mut_iter_method_chain.rs:40:5
|
LL | (&mut **iter).find(|&&x| x == 1);
| ^^^^^^^^^^^^^ help: try: `(*iter).by_ref()`

error: aborting due to 4 previous errors
error: aborting due to 5 previous errors

0 comments on commit 96c400b

Please sign in to comment.