Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FP in external macros for mut_mut lint #7795

Merged
merged 1 commit into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clippy_lints/src/mut_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ impl<'a, 'tcx> intravisit::Visitor<'tcx> for MutVisitor<'a, 'tcx> {
}

fn visit_ty(&mut self, ty: &'tcx hir::Ty<'_>) {
if in_external_macro(self.cx.sess(), ty.span) {
return;
}

if let hir::TyKind::Rptr(
_,
hir::MutTy {
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/auxiliary/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,10 @@ macro_rules! default_numeric_fallback {
let x = 22;
};
}

#[macro_export]
macro_rules! mut_mut {
() => {
let mut_mut_ty: &mut &mut u32 = &mut &mut 1u32;
};
}
10 changes: 10 additions & 0 deletions tests/ui/mut_mut.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
// aux-build:macro_rules.rs

#![allow(unused, clippy::no_effect, clippy::unnecessary_operation)]
#![warn(clippy::mut_mut)]

#[macro_use]
extern crate macro_rules;

fn fun(x: &mut &mut u32) -> bool {
**x > 0
}
Expand Down Expand Up @@ -47,3 +52,8 @@ fn issue939() {
println!(":{}", arg);
}
}

fn issue6922() {
// do not lint from an external macro
mut_mut!();
}
18 changes: 9 additions & 9 deletions tests/ui/mut_mut.stderr
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
error: generally you want to avoid `&mut &mut _` if possible
--> $DIR/mut_mut.rs:4:11
--> $DIR/mut_mut.rs:9:11
|
LL | fn fun(x: &mut &mut u32) -> bool {
| ^^^^^^^^^^^^^
|
= note: `-D clippy::mut-mut` implied by `-D warnings`

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

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

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

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

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

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

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

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