Skip to content

Commit 8bd2a17

Browse files
committed
ignore array from deref_addrof lint
Note that semantics of repeat expr in array are the same
1 parent 4dcab72 commit 8bd2a17

File tree

4 files changed

+8
-11
lines changed

4 files changed

+8
-11
lines changed

clippy_lints/src/reference.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ impl EarlyLintPass for DerefAddrOf {
4848
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &Expr) {
4949
if let ExprKind::Unary(UnOp::Deref, ref deref_target) = e.kind
5050
&& let ExprKind::AddrOf(_, ref mutability, ref addrof_target) = without_parens(deref_target).kind
51+
// NOTE(tesuji): `*&` forces rustc to const-promote the array to `.rodata` section.
52+
// See #12854 for details.
53+
&& !matches!(addrof_target.kind, ExprKind::Array(_))
5154
&& deref_target.span.eq_ctxt(e.span)
5255
&& !addrof_target.span.from_expansion()
5356
{

tests/ui/deref_addrof.fixed

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ fn main() {
4444

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

47-
// do NOT lint for array as sematic differences with/out `*&`.
4847
let _repeat = [0; 64];
49-
let _arr = [0, 1, 2, 3, 4];
48+
// do NOT lint for array as sematic differences with/out `*&`.
49+
let _arr = *&[0, 1, 2, 3, 4];
5050
}
5151

5252
#[derive(Copy, Clone)]

tests/ui/deref_addrof.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ fn main() {
4444

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

47-
// do NOT lint for array as sematic differences with/out `*&`.
4847
let _repeat = *&[0; 64];
48+
// do NOT lint for array as sematic differences with/out `*&`.
4949
let _arr = *&[0, 1, 2, 3, 4];
5050
}
5151

tests/ui/deref_addrof.stderr

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,11 @@ LL | let b = **&aref;
5050
| ^^^^^^ help: try: `aref`
5151

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

58-
error: immediately dereferencing a reference
59-
--> tests/ui/deref_addrof.rs:49:16
60-
|
61-
LL | let _arr = *&[0, 1, 2, 3, 4];
62-
| ^^^^^^^^^^^^^^^^^ help: try: `[0, 1, 2, 3, 4]`
63-
6458
error: immediately dereferencing a reference
6559
--> tests/ui/deref_addrof.rs:57:17
6660
|
@@ -77,5 +71,5 @@ LL | inline!(*&mut $(@expr self))
7771
|
7872
= note: this error originates in the macro `__inline_mac_impl` (in Nightly builds, run with -Z macro-backtrace for more info)
7973

80-
error: aborting due to 12 previous errors
74+
error: aborting due to 11 previous errors
8175

0 commit comments

Comments
 (0)