@@ -218,6 +218,13 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
218218 warnings : self . warnings ,
219219 suggest_unsafe_block : self . suggest_unsafe_block ,
220220 } ;
221+ // params in THIR may be unsafe, e.g. a union pattern.
222+ for param in & inner_thir. params {
223+ if let Some ( param_pat) = param. pat . as_deref ( ) {
224+ inner_visitor. visit_pat ( param_pat) ;
225+ }
226+ }
227+ // Visit the body.
221228 inner_visitor. visit_expr ( & inner_thir[ expr] ) ;
222229 // Unsafe blocks can be used in the inner body, make sure to take it into account
223230 self . safety_context = inner_visitor. safety_context ;
@@ -315,14 +322,15 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
315322 | PatKind :: DerefPattern { .. }
316323 | PatKind :: Range { .. }
317324 | PatKind :: Slice { .. }
318- | PatKind :: Array { .. } => {
325+ | PatKind :: Array { .. }
326+ // Never constitutes a witness of uninhabitedness.
327+ | PatKind :: Never => {
319328 self . requires_unsafe ( pat. span , AccessToUnionField ) ;
320329 return ; // we can return here since this already requires unsafe
321330 }
322- // wildcard/never don 't take anything
331+ // wildcard doesn 't read anything.
323332 PatKind :: Wild |
324- PatKind :: Never |
325- // these just wrap other patterns
333+ // these just wrap other patterns, which we recurse on below.
326334 PatKind :: Or { .. } |
327335 PatKind :: InlineConstant { .. } |
328336 PatKind :: AscribeUserType { .. } |
@@ -1032,6 +1040,13 @@ pub(crate) fn check_unsafety(tcx: TyCtxt<'_>, def: LocalDefId) {
10321040 warnings : & mut warnings,
10331041 suggest_unsafe_block : true ,
10341042 } ;
1043+ // params in THIR may be unsafe, e.g. a union pattern.
1044+ for param in & thir. params {
1045+ if let Some ( param_pat) = param. pat . as_deref ( ) {
1046+ visitor. visit_pat ( param_pat) ;
1047+ }
1048+ }
1049+ // Visit the body.
10351050 visitor. visit_expr ( & thir[ expr] ) ;
10361051
10371052 warnings. sort_by_key ( |w| w. block_span ) ;
0 commit comments