Skip to content
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
3 changes: 3 additions & 0 deletions clippy_lints/src/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ impl EarlyLintPass for DerefAddrOf {
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &Expr) {
if let ExprKind::Unary(UnOp::Deref, ref deref_target) = e.kind
&& let ExprKind::AddrOf(_, ref mutability, ref addrof_target) = without_parens(deref_target).kind
// NOTE(tesuji): `*&` forces rustc to const-promote the array to `.rodata` section.
// See #12854 for details.
&& !matches!(addrof_target.kind, ExprKind::Array(_))
&& deref_target.span.eq_ctxt(e.span)
&& !addrof_target.span.from_expansion()
{
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/deref_addrof.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ fn main() {
let b = *aref;

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

let _repeat = [0; 64];
// do NOT lint for array as sematic differences with/out `*&`.
let _arr = *&[0, 1, 2, 3, 4];
}

#[derive(Copy, Clone)]
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/deref_addrof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ fn main() {
let b = **&aref;

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

let _repeat = *&[0; 64];
// do NOT lint for array as sematic differences with/out `*&`.
let _arr = *&[0, 1, 2, 3, 4];
}

#[derive(Copy, Clone)]
Expand Down
12 changes: 9 additions & 3 deletions tests/ui/deref_addrof.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,26 @@ LL | let b = **&aref;
| ^^^^^^ help: try: `aref`

error: immediately dereferencing a reference
--> tests/ui/deref_addrof.rs:53:17
--> tests/ui/deref_addrof.rs:47:19
|
LL | let _repeat = *&[0; 64];
| ^^^^^^^^^ help: try: `[0; 64]`

error: immediately dereferencing a reference
--> tests/ui/deref_addrof.rs:57:17
|
LL | inline!(*& $(@expr self))
| ^^^^^^^^^^^^^^^^ help: try: `$(@expr self)`
|
= note: this error originates in the macro `__inline_mac_impl` (in Nightly builds, run with -Z macro-backtrace for more info)

error: immediately dereferencing a reference
--> tests/ui/deref_addrof.rs:57:17
--> tests/ui/deref_addrof.rs:61:17
|
LL | inline!(*&mut $(@expr self))
| ^^^^^^^^^^^^^^^^^^^ help: try: `$(@expr self)`
|
= note: this error originates in the macro `__inline_mac_impl` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 10 previous errors
error: aborting due to 11 previous errors