Skip to content

Commit bc9ad84

Browse files
committed
Auto merge of #7795 - ThibsG:MutMut6922, r=giraffate
Fix FP in external macros for `mut_mut` lint Fix FP in `mut_mut` lint when type is defined in external macros. fixes: #6922 changelog: [`mut_mut`] Fix FP when type is defined in external macros
2 parents 9205e3d + 1e18b8a commit bc9ad84

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

clippy_lints/src/mut_mut.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ impl<'a, 'tcx> intravisit::Visitor<'tcx> for MutVisitor<'a, 'tcx> {
8282
}
8383

8484
fn visit_ty(&mut self, ty: &'tcx hir::Ty<'_>) {
85+
if in_external_macro(self.cx.sess(), ty.span) {
86+
return;
87+
}
88+
8589
if let hir::TyKind::Rptr(
8690
_,
8791
hir::MutTy {

tests/ui/auxiliary/macro_rules.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,10 @@ macro_rules! default_numeric_fallback {
113113
let x = 22;
114114
};
115115
}
116+
117+
#[macro_export]
118+
macro_rules! mut_mut {
119+
() => {
120+
let mut_mut_ty: &mut &mut u32 = &mut &mut 1u32;
121+
};
122+
}

tests/ui/mut_mut.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
// aux-build:macro_rules.rs
2+
13
#![allow(unused, clippy::no_effect, clippy::unnecessary_operation)]
24
#![warn(clippy::mut_mut)]
35

6+
#[macro_use]
7+
extern crate macro_rules;
8+
49
fn fun(x: &mut &mut u32) -> bool {
510
**x > 0
611
}
@@ -47,3 +52,8 @@ fn issue939() {
4752
println!(":{}", arg);
4853
}
4954
}
55+
56+
fn issue6922() {
57+
// do not lint from an external macro
58+
mut_mut!();
59+
}

tests/ui/mut_mut.stderr

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
error: generally you want to avoid `&mut &mut _` if possible
2-
--> $DIR/mut_mut.rs:4:11
2+
--> $DIR/mut_mut.rs:9:11
33
|
44
LL | fn fun(x: &mut &mut u32) -> bool {
55
| ^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::mut-mut` implied by `-D warnings`
88

99
error: generally you want to avoid `&mut &mut _` if possible
10-
--> $DIR/mut_mut.rs:20:17
10+
--> $DIR/mut_mut.rs:25:17
1111
|
1212
LL | let mut x = &mut &mut 1u32;
1313
| ^^^^^^^^^^^^^^
1414

1515
error: generally you want to avoid `&mut &mut _` if possible
16-
--> $DIR/mut_mut.rs:14:9
16+
--> $DIR/mut_mut.rs:19:9
1717
|
1818
LL | &mut $p
1919
| ^^^^^^^
@@ -24,37 +24,37 @@ LL | let mut z = mut_ptr!(&mut 3u32);
2424
= note: this error originates in the macro `mut_ptr` (in Nightly builds, run with -Z macro-backtrace for more info)
2525

2626
error: this expression mutably borrows a mutable reference. Consider reborrowing
27-
--> $DIR/mut_mut.rs:22:21
27+
--> $DIR/mut_mut.rs:27:21
2828
|
2929
LL | let mut y = &mut x;
3030
| ^^^^^^
3131

3232
error: generally you want to avoid `&mut &mut _` if possible
33-
--> $DIR/mut_mut.rs:26:32
33+
--> $DIR/mut_mut.rs:31:32
3434
|
3535
LL | let y: &mut &mut u32 = &mut &mut 2;
3636
| ^^^^^^^^^^^
3737

3838
error: generally you want to avoid `&mut &mut _` if possible
39-
--> $DIR/mut_mut.rs:26:16
39+
--> $DIR/mut_mut.rs:31:16
4040
|
4141
LL | let y: &mut &mut u32 = &mut &mut 2;
4242
| ^^^^^^^^^^^^^
4343

4444
error: generally you want to avoid `&mut &mut _` if possible
45-
--> $DIR/mut_mut.rs:31:37
45+
--> $DIR/mut_mut.rs:36:37
4646
|
4747
LL | let y: &mut &mut &mut u32 = &mut &mut &mut 2;
4848
| ^^^^^^^^^^^^^^^^
4949

5050
error: generally you want to avoid `&mut &mut _` if possible
51-
--> $DIR/mut_mut.rs:31:16
51+
--> $DIR/mut_mut.rs:36:16
5252
|
5353
LL | let y: &mut &mut &mut u32 = &mut &mut &mut 2;
5454
| ^^^^^^^^^^^^^^^^^^
5555

5656
error: generally you want to avoid `&mut &mut _` if possible
57-
--> $DIR/mut_mut.rs:31:21
57+
--> $DIR/mut_mut.rs:36:21
5858
|
5959
LL | let y: &mut &mut &mut u32 = &mut &mut &mut 2;
6060
| ^^^^^^^^^^^^^

0 commit comments

Comments
 (0)