about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-09-11 19:42:10 +0000
committerbors <bors@rust-lang.org>2023-09-11 19:42:10 +0000
commit98363cbf6a7c3f8b571a7d92a3c645bb4376e4a6 (patch)
tree64c313e451c99d94b958803ab3176b5eab2b0581
parent8c48b936ccf5cf62647420e604bb2cae155268a4 (diff)
parentc548d11041e074bf32e1938bd34625eb1acdfe1d (diff)
downloadrust-98363cbf6a7c3f8b571a7d92a3c645bb4376e4a6.tar.gz
rust-98363cbf6a7c3f8b571a7d92a3c645bb4376e4a6.zip
Auto merge of #11477 - samueltardieu:11474, r=xFrednet
Auto deref does not apply on union field

changelog: [`explicit_auto_deref`]: do not suggest propose to auto-dereference an union field

Fix #11474
-rw-r--r--clippy_lints/src/dereference.rs7
-rw-r--r--tests/ui/explicit_auto_deref.fixed22
-rw-r--r--tests/ui/explicit_auto_deref.rs22
3 files changed, 51 insertions, 0 deletions
diff --git a/clippy_lints/src/dereference.rs b/clippy_lints/src/dereference.rs
index 1b23f01913f..8090f821d1f 100644
--- a/clippy_lints/src/dereference.rs
+++ b/clippy_lints/src/dereference.rs
@@ -1399,6 +1399,13 @@ fn report<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, state: State, data
                 return;
             }
 
+            if let ExprKind::Field(parent_expr, _) = expr.kind
+                && let ty::Adt(adt, _) = cx.typeck_results().expr_ty(parent_expr).kind()
+                && adt.is_union()
+            {
+                // Auto deref does not apply on union field
+                return;
+            }
             span_lint_hir_and_then(
                 cx,
                 EXPLICIT_AUTO_DEREF,
diff --git a/tests/ui/explicit_auto_deref.fixed b/tests/ui/explicit_auto_deref.fixed
index d046a926935..12158d0d12a 100644
--- a/tests/ui/explicit_auto_deref.fixed
+++ b/tests/ui/explicit_auto_deref.fixed
@@ -299,4 +299,26 @@ fn main() {
         Some(x) => x,
         None => panic!(),
     };
+
+    // Issue #11474
+    pub struct Variant {
+        pub anonymous: Variant0,
+    }
+
+    pub union Variant0 {
+        pub anonymous: std::mem::ManuallyDrop<Variant00>,
+    }
+
+    pub struct Variant00 {
+        pub anonymous: Variant000,
+    }
+
+    pub union Variant000 {
+        pub val: i32,
+    }
+
+    unsafe {
+        let mut p = core::mem::zeroed::<Variant>();
+        (*p.anonymous.anonymous).anonymous.val = 1;
+    }
 }
diff --git a/tests/ui/explicit_auto_deref.rs b/tests/ui/explicit_auto_deref.rs
index 3dd08e487f8..dec021c1834 100644
--- a/tests/ui/explicit_auto_deref.rs
+++ b/tests/ui/explicit_auto_deref.rs
@@ -299,4 +299,26 @@ fn main() {
         Some(x) => &mut *x,
         None => panic!(),
     };
+
+    // Issue #11474
+    pub struct Variant {
+        pub anonymous: Variant0,
+    }
+
+    pub union Variant0 {
+        pub anonymous: std::mem::ManuallyDrop<Variant00>,
+    }
+
+    pub struct Variant00 {
+        pub anonymous: Variant000,
+    }
+
+    pub union Variant000 {
+        pub val: i32,
+    }
+
+    unsafe {
+        let mut p = core::mem::zeroed::<Variant>();
+        (*p.anonymous.anonymous).anonymous.val = 1;
+    }
 }