diff --git a/clippy_lints/src/reference.rs b/clippy_lints/src/reference.rs index 22ae7a291d00e..b24483723700c 100644 --- a/clippy_lints/src/reference.rs +++ b/clippy_lints/src/reference.rs @@ -50,6 +50,7 @@ impl EarlyLintPass for DerefAddrOf { if_chain! { if let ExprKind::Unary(UnOp::Deref, ref deref_target) = e.kind; if let ExprKind::AddrOf(_, ref mutability, ref addrof_target) = without_parens(deref_target).kind; + if deref_target.span.ctxt() == e.span.ctxt(); if !addrof_target.span.from_expansion(); then { let mut applicability = Applicability::MachineApplicable; diff --git a/tests/ui/deref_addrof.fixed b/tests/ui/deref_addrof.fixed index 0029fc673f110..2f489deb1ee1f 100644 --- a/tests/ui/deref_addrof.fixed +++ b/tests/ui/deref_addrof.fixed @@ -37,6 +37,8 @@ fn main() { let b = &a; let b = *aref; + + let _ = unsafe { *core::ptr::addr_of!(a) }; } #[rustfmt::skip] diff --git a/tests/ui/deref_addrof.rs b/tests/ui/deref_addrof.rs index f2f02dd5e723d..49f360b9a7f9e 100644 --- a/tests/ui/deref_addrof.rs +++ b/tests/ui/deref_addrof.rs @@ -37,6 +37,8 @@ fn main() { let b = *&&a; let b = **&aref; + + let _ = unsafe { *core::ptr::addr_of!(a) }; } #[rustfmt::skip] diff --git a/tests/ui/deref_addrof.stderr b/tests/ui/deref_addrof.stderr index 5bc1cbfa21510..75371fcdb9677 100644 --- a/tests/ui/deref_addrof.stderr +++ b/tests/ui/deref_addrof.stderr @@ -49,7 +49,7 @@ LL | let b = **&aref; | ^^^^^^ help: try this: `aref` error: immediately dereferencing a reference - --> $DIR/deref_addrof.rs:45:9 + --> $DIR/deref_addrof.rs:47:9 | LL | *& $visitor | ^^^^^^^^^^^ help: try this: `$visitor` @@ -60,7 +60,7 @@ LL | m!(self) = note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info) error: immediately dereferencing a reference - --> $DIR/deref_addrof.rs:52:9 + --> $DIR/deref_addrof.rs:54:9 | LL | *& mut $visitor | ^^^^^^^^^^^^^^^ help: try this: `$visitor`