diff --git a/clippy_lints/src/mut_mut.rs b/clippy_lints/src/mut_mut.rs index d98c70e7f5a8..78053d91b54a 100644 --- a/clippy_lints/src/mut_mut.rs +++ b/clippy_lints/src/mut_mut.rs @@ -1,5 +1,4 @@ use clippy_utils::diagnostics::{span_lint, span_lint_hir}; -use clippy_utils::higher; use rustc_hir::{self as hir, AmbigArg, intravisit}; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_middle::ty; @@ -55,20 +54,11 @@ pub struct MutVisitor<'a, 'tcx> { impl<'tcx> intravisit::Visitor<'tcx> for MutVisitor<'_, 'tcx> { fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) { - if expr.span.in_external_macro(self.cx.sess().source_map()) { + if expr.span.from_expansion() { return; } - if let Some(higher::ForLoop { arg, body, .. }) = higher::ForLoop::hir(expr) { - // A `for` loop lowers to: - // ```rust - // match ::std::iter::Iterator::next(&mut iter) { - // // ^^^^ - // ``` - // Let's ignore the generated code. - intravisit::walk_expr(self, arg); - intravisit::walk_expr(self, body); - } else if let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Mut, e) = expr.kind { + if let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Mut, e) = expr.kind { if let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Mut, _) = e.kind { span_lint_hir( self.cx, diff --git a/tests/ui/mut_mut.rs b/tests/ui/mut_mut.rs index bbcdbc89b6a4..68cecfd53a58 100644 --- a/tests/ui/mut_mut.rs +++ b/tests/ui/mut_mut.rs @@ -53,18 +53,25 @@ fn main() { } let mut z = inline!(&mut $(&mut 3u32)); - //~^ mut_mut } fn issue939() { let array = [5, 6, 7, 8, 9]; let mut args = array.iter().skip(2); for &arg in &mut args { + // make sure that we still check inside the block, even if we skip `&mut args` because of + // it coming from the expansion of `for-in` syntax + let a = &mut &mut 2; + //~^ mut_mut println!("{}", arg); } let args = &mut args; for arg in args { + // make sure that we still check inside the block, even if we skip `&mut args` because of + // it coming from the expansion of `for-in` syntax + let a = &mut &mut 2; + //~^ mut_mut println!(":{}", arg); } } diff --git a/tests/ui/mut_mut.stderr b/tests/ui/mut_mut.stderr index 74b0c9ba145a..04c935344e01 100644 --- a/tests/ui/mut_mut.stderr +++ b/tests/ui/mut_mut.stderr @@ -13,14 +13,6 @@ error: generally you want to avoid `&mut &mut _` if possible LL | let mut x = &mut &mut 1u32; | ^^^^^^^^^^^^^^ -error: generally you want to avoid `&mut &mut _` if possible - --> tests/ui/mut_mut.rs:55:25 - | -LL | let mut z = inline!(&mut $(&mut 3u32)); - | ^ - | - = note: this error originates in the macro `__inline_mac_fn_main` (in Nightly builds, run with -Z macro-backtrace for more info) - error: this expression mutably borrows a mutable reference. Consider reborrowing --> tests/ui/mut_mut.rs:36:21 | @@ -57,5 +49,17 @@ error: generally you want to avoid `&mut &mut _` if possible LL | let y: &mut &mut &mut u32 = &mut &mut &mut 2; | ^^^^^^^^^^^^^ -error: aborting due to 9 previous errors +error: generally you want to avoid `&mut &mut _` if possible + --> tests/ui/mut_mut.rs:64:17 + | +LL | let a = &mut &mut 2; + | ^^^^^^^^^^^ + +error: generally you want to avoid `&mut &mut _` if possible + --> tests/ui/mut_mut.rs:73:17 + | +LL | let a = &mut &mut 2; + | ^^^^^^^^^^^ + +error: aborting due to 10 previous errors