@@ -218,6 +218,13 @@ impl<'tcx> UnsafetyVisitor<'_, 'tcx> {
218
218
warnings : self . warnings ,
219
219
suggest_unsafe_block : self . suggest_unsafe_block ,
220
220
} ;
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.
221
228
inner_visitor. visit_expr ( & inner_thir[ expr] ) ;
222
229
// Unsafe blocks can be used in the inner body, make sure to take it into account
223
230
self . safety_context = inner_visitor. safety_context ;
@@ -315,14 +322,15 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
315
322
| PatKind :: DerefPattern { .. }
316
323
| PatKind :: Range { .. }
317
324
| PatKind :: Slice { .. }
318
- | PatKind :: Array { .. } => {
325
+ | PatKind :: Array { .. }
326
+ // Never constitutes a witness of uninhabitedness.
327
+ | PatKind :: Never => {
319
328
self . requires_unsafe ( pat. span , AccessToUnionField ) ;
320
329
return ; // we can return here since this already requires unsafe
321
330
}
322
- // wildcard/never don 't take anything
331
+ // wildcard doesn 't read anything.
323
332
PatKind :: Wild |
324
- PatKind :: Never |
325
- // these just wrap other patterns
333
+ // these just wrap other patterns, which we recurse on below.
326
334
PatKind :: Or { .. } |
327
335
PatKind :: InlineConstant { .. } |
328
336
PatKind :: AscribeUserType { .. } |
@@ -1032,6 +1040,13 @@ pub(crate) fn check_unsafety(tcx: TyCtxt<'_>, def: LocalDefId) {
1032
1040
warnings : & mut warnings,
1033
1041
suggest_unsafe_block : true ,
1034
1042
} ;
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.
1035
1050
visitor. visit_expr ( & thir[ expr] ) ;
1036
1051
1037
1052
warnings. sort_by_key ( |w| w. block_span ) ;
0 commit comments