Skip to content

Commit

Permalink
Auto merge of rust-lang#8268 - Jarcho:deref_addrof_8247, r=flip1995
Browse files Browse the repository at this point in the history
Fix `deref_addrof`

fixes rust-lang#8247

This would supersede rust-lang#8259

changelog: Don't lint `deref_addrof` when the dereference and the borrow occur in different contexts
  • Loading branch information
bors committed Jan 12, 2022
2 parents 88f5be2 + d32277d commit 37e9985
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions clippy_lints/src/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/deref_addrof.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ fn main() {
let b = &a;

let b = *aref;

let _ = unsafe { *core::ptr::addr_of!(a) };
}

#[rustfmt::skip]
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/deref_addrof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ fn main() {
let b = *&&a;

let b = **&aref;

let _ = unsafe { *core::ptr::addr_of!(a) };
}

#[rustfmt::skip]
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/deref_addrof.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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`
Expand Down

0 comments on commit 37e9985

Please sign in to comment.