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
This commit is contained in:
bors 2023-09-11 19:42:10 +00:00
commit 98363cbf6a
3 changed files with 51 additions and 0 deletions

View file

@ -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,

View file

@ -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;
}
}

View file

@ -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;
}
}