Skip to content

Commit b2f9c4c

Browse files
committed
Auto merge of #12442 - cookie-s:fix-mutmut-duplicate-diags, r=y21
[`mut_mut`]: Fix duplicate diags Relates to #12379 The `mut_mut` lint produced two diagnostics for each `mut mut` pattern in `ty` inside `block`s because `MutVisitor::visit_ty` was called from `MutMut::check_ty` and `MutMut::check_block` independently. This PR fixes the issue. --- changelog: [`mut_mut`]: Fix duplicate diagnostics
2 parents 099e2c0 + 4e95b4a commit b2f9c4c

File tree

3 files changed

+36
-45
lines changed

3 files changed

+36
-45
lines changed

Diff for: clippy_lints/src/mut_mut.rs

+27-35
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,34 @@ impl<'tcx> LateLintPass<'tcx> for MutMut {
3535
}
3636

3737
fn check_ty(&mut self, cx: &LateContext<'tcx>, ty: &'tcx hir::Ty<'_>) {
38-
use rustc_hir::intravisit::Visitor;
38+
if in_external_macro(cx.sess(), ty.span) {
39+
return;
40+
}
3941

40-
MutVisitor { cx }.visit_ty(ty);
42+
if let hir::TyKind::Ref(
43+
_,
44+
hir::MutTy {
45+
ty: pty,
46+
mutbl: hir::Mutability::Mut,
47+
},
48+
) = ty.kind
49+
{
50+
if let hir::TyKind::Ref(
51+
_,
52+
hir::MutTy {
53+
mutbl: hir::Mutability::Mut,
54+
..
55+
},
56+
) = pty.kind
57+
{
58+
span_lint(
59+
cx,
60+
MUT_MUT,
61+
ty.span,
62+
"generally you want to avoid `&mut &mut _` if possible",
63+
);
64+
}
65+
}
4166
}
4267
}
4368

@@ -80,37 +105,4 @@ impl<'a, 'tcx> intravisit::Visitor<'tcx> for MutVisitor<'a, 'tcx> {
80105
}
81106
}
82107
}
83-
84-
fn visit_ty(&mut self, ty: &'tcx hir::Ty<'_>) {
85-
if in_external_macro(self.cx.sess(), ty.span) {
86-
return;
87-
}
88-
89-
if let hir::TyKind::Ref(
90-
_,
91-
hir::MutTy {
92-
ty: pty,
93-
mutbl: hir::Mutability::Mut,
94-
},
95-
) = ty.kind
96-
{
97-
if let hir::TyKind::Ref(
98-
_,
99-
hir::MutTy {
100-
mutbl: hir::Mutability::Mut,
101-
..
102-
},
103-
) = pty.kind
104-
{
105-
span_lint(
106-
self.cx,
107-
MUT_MUT,
108-
ty.span,
109-
"generally you want to avoid `&mut &mut _` if possible",
110-
);
111-
}
112-
}
113-
114-
intravisit::walk_ty(self, ty);
115-
}
116108
}

Diff for: tests/ui/mut_mut.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@aux-build:proc_macros.rs
2-
//@compile-flags: -Zdeduplicate-diagnostics=yes
32

43
#![warn(clippy::mut_mut)]
54
#![allow(unused)]

Diff for: tests/ui/mut_mut.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: generally you want to avoid `&mut &mut _` if possible
2-
--> tests/ui/mut_mut.rs:16:11
2+
--> tests/ui/mut_mut.rs:15:11
33
|
44
LL | fn fun(x: &mut &mut u32) -> bool {
55
| ^^^^^^^^^^^^^
@@ -8,51 +8,51 @@ LL | fn fun(x: &mut &mut u32) -> bool {
88
= help: to override `-D warnings` add `#[allow(clippy::mut_mut)]`
99

1010
error: generally you want to avoid `&mut &mut _` if possible
11-
--> tests/ui/mut_mut.rs:33:17
11+
--> tests/ui/mut_mut.rs:32:17
1212
|
1313
LL | let mut x = &mut &mut 1u32;
1414
| ^^^^^^^^^^^^^^
1515

1616
error: generally you want to avoid `&mut &mut _` if possible
17-
--> tests/ui/mut_mut.rs:48:25
17+
--> tests/ui/mut_mut.rs:47:25
1818
|
1919
LL | let mut z = inline!(&mut $(&mut 3u32));
2020
| ^
2121
|
2222
= note: this error originates in the macro `__inline_mac_fn_main` (in Nightly builds, run with -Z macro-backtrace for more info)
2323

2424
error: this expression mutably borrows a mutable reference. Consider reborrowing
25-
--> tests/ui/mut_mut.rs:35:21
25+
--> tests/ui/mut_mut.rs:34:21
2626
|
2727
LL | let mut y = &mut x;
2828
| ^^^^^^
2929

3030
error: generally you want to avoid `&mut &mut _` if possible
31-
--> tests/ui/mut_mut.rs:39:32
31+
--> tests/ui/mut_mut.rs:38:32
3232
|
3333
LL | let y: &mut &mut u32 = &mut &mut 2;
3434
| ^^^^^^^^^^^
3535

3636
error: generally you want to avoid `&mut &mut _` if possible
37-
--> tests/ui/mut_mut.rs:39:16
37+
--> tests/ui/mut_mut.rs:38:16
3838
|
3939
LL | let y: &mut &mut u32 = &mut &mut 2;
4040
| ^^^^^^^^^^^^^
4141

4242
error: generally you want to avoid `&mut &mut _` if possible
43-
--> tests/ui/mut_mut.rs:44:37
43+
--> tests/ui/mut_mut.rs:43:37
4444
|
4545
LL | let y: &mut &mut &mut u32 = &mut &mut &mut 2;
4646
| ^^^^^^^^^^^^^^^^
4747

4848
error: generally you want to avoid `&mut &mut _` if possible
49-
--> tests/ui/mut_mut.rs:44:16
49+
--> tests/ui/mut_mut.rs:43:16
5050
|
5151
LL | let y: &mut &mut &mut u32 = &mut &mut &mut 2;
5252
| ^^^^^^^^^^^^^^^^^^
5353

5454
error: generally you want to avoid `&mut &mut _` if possible
55-
--> tests/ui/mut_mut.rs:44:21
55+
--> tests/ui/mut_mut.rs:43:21
5656
|
5757
LL | let y: &mut &mut &mut u32 = &mut &mut &mut 2;
5858
| ^^^^^^^^^^^^^

0 commit comments

Comments
 (0)