-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
163 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::in_macro; | ||
use clippy_utils::source::snippet_with_context; | ||
use clippy_utils::ty::implements_trait; | ||
use if_chain::if_chain; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability, UnOp}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::sym; | ||
|
||
use super::REF_MUT_ITER_METHOD_CHAIN; | ||
|
||
pub(crate) fn check(cx: &LateContext<'_>, self_arg: &Expr<'_>) { | ||
if_chain! { | ||
if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, base_expr) = self_arg.kind; | ||
if !in_macro(self_arg.span); | ||
if let Some(&iter_trait) = cx.tcx.all_diagnostic_items(()).get(&sym::Iterator); | ||
if implements_trait(cx, cx.typeck_results().expr_ty(base_expr).peel_refs(), iter_trait, &[]); | ||
then { | ||
let snip_span = match base_expr.kind { | ||
ExprKind::Unary(UnOp::Deref, e) if cx.typeck_results().expr_ty(e).is_ref() && !in_macro(base_expr.span) | ||
=> e.span, | ||
_ => base_expr.span, | ||
}; | ||
let mut app = Applicability::MachineApplicable; | ||
span_lint_and_sugg( | ||
cx, | ||
REF_MUT_ITER_METHOD_CHAIN, | ||
self_arg.span, | ||
"use of `&mut` on an iterator in a method chain", | ||
"try", | ||
format!( | ||
"{}.by_ref()", | ||
snippet_with_context(cx, snip_span, self_arg.span.ctxt(), "..", &mut app).0, | ||
), | ||
app, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// run-rustfix | ||
#![warn(clippy::ref_mut_iter_method_chain)] | ||
|
||
macro_rules! m { | ||
($i:ident) => { | ||
$i | ||
}; | ||
(&mut $i:ident) => { | ||
&mut $i | ||
}; | ||
(($i:expr).$m:ident($arg:expr)) => { | ||
($i).$m($arg) | ||
}; | ||
} | ||
|
||
fn main() { | ||
let mut iter = [0, 1, 2].iter(); | ||
let _ = iter.by_ref().find(|&&x| x == 1); | ||
let _ = m!(iter).by_ref().find(|&&x| x == 1); | ||
|
||
// Don't lint. `&mut` comes from macro expansion. | ||
let _ = m!(&mut iter).find(|&&x| x == 1); | ||
|
||
// Don't lint. Method call from expansion | ||
let _ = m!((&mut iter).find(|&&x| x == 1)); | ||
|
||
// Don't lint. No method chain. | ||
for &x in &mut iter { | ||
print!("{}", x) | ||
} | ||
|
||
let iter = &mut iter; | ||
iter.by_ref().find(|&&x| x == 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// run-rustfix | ||
#![warn(clippy::ref_mut_iter_method_chain)] | ||
|
||
macro_rules! m { | ||
($i:ident) => { | ||
$i | ||
}; | ||
(&mut $i:ident) => { | ||
&mut $i | ||
}; | ||
(($i:expr).$m:ident($arg:expr)) => { | ||
($i).$m($arg) | ||
}; | ||
} | ||
|
||
fn main() { | ||
let mut iter = [0, 1, 2].iter(); | ||
let _ = (&mut iter).find(|&&x| x == 1); | ||
let _ = (&mut m!(iter)).find(|&&x| x == 1); | ||
|
||
// Don't lint. `&mut` comes from macro expansion. | ||
let _ = m!(&mut iter).find(|&&x| x == 1); | ||
|
||
// Don't lint. Method call from expansion | ||
let _ = m!((&mut iter).find(|&&x| x == 1)); | ||
|
||
// Don't lint. No method chain. | ||
for &x in &mut iter { | ||
print!("{}", x) | ||
} | ||
|
||
let iter = &mut iter; | ||
(&mut *iter).find(|&&x| x == 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
error: use of `&mut` on an iterator in a method chain | ||
--> $DIR/ref_mut_iter_method_chain.rs:18:13 | ||
| | ||
LL | let _ = (&mut iter).find(|&&x| x == 1); | ||
| ^^^^^^^^^^^ help: try: `iter.by_ref()` | ||
| | ||
= note: `-D clippy::ref-mut-iter-method-chain` implied by `-D warnings` | ||
|
||
error: use of `&mut` on an iterator in a method chain | ||
--> $DIR/ref_mut_iter_method_chain.rs:19:13 | ||
| | ||
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 | ||
| | ||
LL | (&mut *iter).find(|&&x| x == 1); | ||
| ^^^^^^^^^^^^ help: try: `iter.by_ref()` | ||
|
||
error: aborting due to 3 previous errors | ||
|