Skip to content

Commit 03654ba

Browse files
committed
Auto merge of #12864 - tesuji:non-no-effect, r=y21
ignore array from `deref_addrof` lint Split from #12854 changelog: ignore array from `deref_addrof` lint r? y21
2 parents c9139bd + 8bd2a17 commit 03654ba

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ fn main() {
4343
let b = *aref;
4444

4545
let _ = unsafe { *core::ptr::addr_of!(a) };
46+
47+
let _repeat = [0; 64];
48+
// do NOT lint for array as sematic differences with/out `*&`.
49+
let _arr = *&[0, 1, 2, 3, 4];
4650
}
4751

4852
#[derive(Copy, Clone)]

tests/ui/deref_addrof.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ fn main() {
4343
let b = **&aref;
4444

4545
let _ = unsafe { *core::ptr::addr_of!(a) };
46+
47+
let _repeat = *&[0; 64];
48+
// do NOT lint for array as sematic differences with/out `*&`.
49+
let _arr = *&[0, 1, 2, 3, 4];
4650
}
4751

4852
#[derive(Copy, Clone)]

tests/ui/deref_addrof.stderr

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,26 @@ LL | let b = **&aref;
5050
| ^^^^^^ help: try: `aref`
5151

5252
error: immediately dereferencing a reference
53-
--> tests/ui/deref_addrof.rs:53:17
53+
--> tests/ui/deref_addrof.rs:47:19
54+
|
55+
LL | let _repeat = *&[0; 64];
56+
| ^^^^^^^^^ help: try: `[0; 64]`
57+
58+
error: immediately dereferencing a reference
59+
--> tests/ui/deref_addrof.rs:57:17
5460
|
5561
LL | inline!(*& $(@expr self))
5662
| ^^^^^^^^^^^^^^^^ help: try: `$(@expr self)`
5763
|
5864
= note: this error originates in the macro `__inline_mac_impl` (in Nightly builds, run with -Z macro-backtrace for more info)
5965

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

68-
error: aborting due to 10 previous errors
74+
error: aborting due to 11 previous errors
6975

0 commit comments

Comments
 (0)