Skip to content

Commit 98363cb

Browse files
committed
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
2 parents 8c48b93 + c548d11 commit 98363cb

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

clippy_lints/src/dereference.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,13 @@ fn report<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, state: State, data
13991399
return;
14001400
}
14011401

1402+
if let ExprKind::Field(parent_expr, _) = expr.kind
1403+
&& let ty::Adt(adt, _) = cx.typeck_results().expr_ty(parent_expr).kind()
1404+
&& adt.is_union()
1405+
{
1406+
// Auto deref does not apply on union field
1407+
return;
1408+
}
14021409
span_lint_hir_and_then(
14031410
cx,
14041411
EXPLICIT_AUTO_DEREF,

tests/ui/explicit_auto_deref.fixed

+22
Original file line numberDiff line numberDiff line change
@@ -299,4 +299,26 @@ fn main() {
299299
Some(x) => x,
300300
None => panic!(),
301301
};
302+
303+
// Issue #11474
304+
pub struct Variant {
305+
pub anonymous: Variant0,
306+
}
307+
308+
pub union Variant0 {
309+
pub anonymous: std::mem::ManuallyDrop<Variant00>,
310+
}
311+
312+
pub struct Variant00 {
313+
pub anonymous: Variant000,
314+
}
315+
316+
pub union Variant000 {
317+
pub val: i32,
318+
}
319+
320+
unsafe {
321+
let mut p = core::mem::zeroed::<Variant>();
322+
(*p.anonymous.anonymous).anonymous.val = 1;
323+
}
302324
}

tests/ui/explicit_auto_deref.rs

+22
Original file line numberDiff line numberDiff line change
@@ -299,4 +299,26 @@ fn main() {
299299
Some(x) => &mut *x,
300300
None => panic!(),
301301
};
302+
303+
// Issue #11474
304+
pub struct Variant {
305+
pub anonymous: Variant0,
306+
}
307+
308+
pub union Variant0 {
309+
pub anonymous: std::mem::ManuallyDrop<Variant00>,
310+
}
311+
312+
pub struct Variant00 {
313+
pub anonymous: Variant000,
314+
}
315+
316+
pub union Variant000 {
317+
pub val: i32,
318+
}
319+
320+
unsafe {
321+
let mut p = core::mem::zeroed::<Variant>();
322+
(*p.anonymous.anonymous).anonymous.val = 1;
323+
}
302324
}

0 commit comments

Comments
 (0)