From 8bd2a17dfe1b5089b942c44db6b152f5d4b30a3e Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Wed, 29 May 2024 09:19:33 +0700 Subject: [PATCH] ignore array from `deref_addrof` lint Note that semantics of repeat expr in array are the same --- clippy_lints/src/reference.rs | 3 +++ tests/ui/deref_addrof.fixed | 4 ++-- tests/ui/deref_addrof.rs | 2 +- tests/ui/deref_addrof.stderr | 10 ++-------- 4 files changed, 8 insertions(+), 11 deletions(-) diff --git a/clippy_lints/src/reference.rs b/clippy_lints/src/reference.rs index 16086ba6637..8f32cf5f2a1 100644 --- a/clippy_lints/src/reference.rs +++ b/clippy_lints/src/reference.rs @@ -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() { diff --git a/tests/ui/deref_addrof.fixed b/tests/ui/deref_addrof.fixed index db7c44c4c63..b6278c6ca8a 100644 --- a/tests/ui/deref_addrof.fixed +++ b/tests/ui/deref_addrof.fixed @@ -44,9 +44,9 @@ fn main() { let _ = unsafe { *core::ptr::addr_of!(a) }; - // do NOT lint for array as sematic differences with/out `*&`. let _repeat = [0; 64]; - let _arr = [0, 1, 2, 3, 4]; + // do NOT lint for array as sematic differences with/out `*&`. + let _arr = *&[0, 1, 2, 3, 4]; } #[derive(Copy, Clone)] diff --git a/tests/ui/deref_addrof.rs b/tests/ui/deref_addrof.rs index 79b82a76a13..572b0fdb102 100644 --- a/tests/ui/deref_addrof.rs +++ b/tests/ui/deref_addrof.rs @@ -44,8 +44,8 @@ fn main() { let _ = unsafe { *core::ptr::addr_of!(a) }; - // do NOT lint for array as sematic differences with/out `*&`. let _repeat = *&[0; 64]; + // do NOT lint for array as sematic differences with/out `*&`. let _arr = *&[0, 1, 2, 3, 4]; } diff --git a/tests/ui/deref_addrof.stderr b/tests/ui/deref_addrof.stderr index 856d775d932..20069f746c8 100644 --- a/tests/ui/deref_addrof.stderr +++ b/tests/ui/deref_addrof.stderr @@ -50,17 +50,11 @@ LL | let b = **&aref; | ^^^^^^ help: try: `aref` error: immediately dereferencing a reference - --> tests/ui/deref_addrof.rs:48:19 + --> 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:49:16 - | -LL | let _arr = *&[0, 1, 2, 3, 4]; - | ^^^^^^^^^^^^^^^^^ help: try: `[0, 1, 2, 3, 4]` - error: immediately dereferencing a reference --> tests/ui/deref_addrof.rs:57:17 | @@ -77,5 +71,5 @@ LL | inline!(*&mut $(@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 12 previous errors +error: aborting due to 11 previous errors