Skip to content

Commit 5982c1a

Browse files
committed
mut_mut: simplify from-expansion check
1 parent 32a216e commit 5982c1a

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

clippy_lints/src/mut_mut.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use clippy_utils::diagnostics::{span_lint, span_lint_hir};
2-
use clippy_utils::higher;
32
use rustc_hir::{self as hir, AmbigArg, intravisit};
43
use rustc_lint::{LateContext, LateLintPass, LintContext};
54
use rustc_middle::ty;
@@ -55,20 +54,11 @@ pub struct MutVisitor<'a, 'tcx> {
5554

5655
impl<'tcx> intravisit::Visitor<'tcx> for MutVisitor<'_, 'tcx> {
5756
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) {
58-
if expr.span.in_external_macro(self.cx.sess().source_map()) {
57+
if expr.span.from_expansion() || expr.span.in_external_macro(self.cx.sess().source_map()) {
5958
return;
6059
}
6160

62-
if let Some(higher::ForLoop { arg, body, .. }) = higher::ForLoop::hir(expr) {
63-
// A `for` loop lowers to:
64-
// ```rust
65-
// match ::std::iter::Iterator::next(&mut iter) {
66-
// // ^^^^
67-
// ```
68-
// Let's ignore the generated code.
69-
intravisit::walk_expr(self, arg);
70-
intravisit::walk_expr(self, body);
71-
} else if let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Mut, e) = expr.kind {
61+
if let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Mut, e) = expr.kind {
7262
if let hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Mut, _) = e.kind {
7363
span_lint_hir(
7464
self.cx,

tests/ui/mut_mut.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,25 @@ fn main() {
5353
}
5454

5555
let mut z = inline!(&mut $(&mut 3u32));
56-
//~^ mut_mut
5756
}
5857

5958
fn issue939() {
6059
let array = [5, 6, 7, 8, 9];
6160
let mut args = array.iter().skip(2);
6261
for &arg in &mut args {
62+
// make sure that we still check inside the block, even if we skip `&mut args` because of
63+
// it coming from the expansion of `for-in` syntax
64+
let a = &mut &mut 2;
65+
//~^ mut_mut
6366
println!("{}", arg);
6467
}
6568

6669
let args = &mut args;
6770
for arg in args {
71+
// make sure that we still check inside the block, even if we skip `&mut args` because of
72+
// it coming from the expansion of `for-in` syntax
73+
let a = &mut &mut 2;
74+
//~^ mut_mut
6875
println!(":{}", arg);
6976
}
7077
}

tests/ui/mut_mut.stderr

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,6 @@ error: generally you want to avoid `&mut &mut _` if possible
1313
LL | let mut x = &mut &mut 1u32;
1414
| ^^^^^^^^^^^^^^
1515

16-
error: generally you want to avoid `&mut &mut _` if possible
17-
--> tests/ui/mut_mut.rs:55:25
18-
|
19-
LL | let mut z = inline!(&mut $(&mut 3u32));
20-
| ^
21-
|
22-
= note: this error originates in the macro `__inline_mac_fn_main` (in Nightly builds, run with -Z macro-backtrace for more info)
23-
2416
error: this expression mutably borrows a mutable reference. Consider reborrowing
2517
--> tests/ui/mut_mut.rs:36:21
2618
|
@@ -57,5 +49,17 @@ error: generally you want to avoid `&mut &mut _` if possible
5749
LL | let y: &mut &mut &mut u32 = &mut &mut &mut 2;
5850
| ^^^^^^^^^^^^^
5951

60-
error: aborting due to 9 previous errors
52+
error: generally you want to avoid `&mut &mut _` if possible
53+
--> tests/ui/mut_mut.rs:64:17
54+
|
55+
LL | let a = &mut &mut 2;
56+
| ^^^^^^^^^^^
57+
58+
error: generally you want to avoid `&mut &mut _` if possible
59+
--> tests/ui/mut_mut.rs:73:17
60+
|
61+
LL | let a = &mut &mut 2;
62+
| ^^^^^^^^^^^
63+
64+
error: aborting due to 10 previous errors
6165

0 commit comments

Comments
 (0)