]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_transform/src/check_unsafety.rs
Rollup merge of #99139 - jyn514:dist-tool-help, r=Mark-Simulacrum
[rust.git] / compiler / rustc_mir_transform / src / check_unsafety.rs
1 use rustc_data_structures::fx::FxHashMap;
2 use rustc_errors::struct_span_err;
3 use rustc_hir as hir;
4 use rustc_hir::def_id::{DefId, LocalDefId};
5 use rustc_hir::hir_id::HirId;
6 use rustc_hir::intravisit;
7 use rustc_middle::mir::visit::{MutatingUseContext, PlaceContext, Visitor};
8 use rustc_middle::ty::query::Providers;
9 use rustc_middle::ty::{self, TyCtxt};
10 use rustc_middle::{lint, mir::*};
11 use rustc_session::lint::builtin::{UNSAFE_OP_IN_UNSAFE_FN, UNUSED_UNSAFE};
12 use rustc_session::lint::Level;
13
14 use std::collections::hash_map;
15 use std::ops::Bound;
16
17 pub struct UnsafetyChecker<'a, 'tcx> {
18     body: &'a Body<'tcx>,
19     body_did: LocalDefId,
20     violations: Vec<UnsafetyViolation>,
21     source_info: SourceInfo,
22     tcx: TyCtxt<'tcx>,
23     param_env: ty::ParamEnv<'tcx>,
24
25     /// Used `unsafe` blocks in this function. This is used for the "unused_unsafe" lint.
26     ///
27     /// The keys are the used `unsafe` blocks, the UnusedUnsafeKind indicates whether
28     /// or not any of the usages happen at a place that doesn't allow `unsafe_op_in_unsafe_fn`.
29     used_unsafe_blocks: FxHashMap<HirId, UsedUnsafeBlockData>,
30 }
31
32 impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
33     fn new(
34         body: &'a Body<'tcx>,
35         body_did: LocalDefId,
36         tcx: TyCtxt<'tcx>,
37         param_env: ty::ParamEnv<'tcx>,
38     ) -> Self {
39         Self {
40             body,
41             body_did,
42             violations: vec![],
43             source_info: SourceInfo::outermost(body.span),
44             tcx,
45             param_env,
46             used_unsafe_blocks: Default::default(),
47         }
48     }
49 }
50
51 impl<'tcx> Visitor<'tcx> for UnsafetyChecker<'_, 'tcx> {
52     fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
53         self.source_info = terminator.source_info;
54         match terminator.kind {
55             TerminatorKind::Goto { .. }
56             | TerminatorKind::SwitchInt { .. }
57             | TerminatorKind::Drop { .. }
58             | TerminatorKind::Yield { .. }
59             | TerminatorKind::Assert { .. }
60             | TerminatorKind::DropAndReplace { .. }
61             | TerminatorKind::GeneratorDrop
62             | TerminatorKind::Resume
63             | TerminatorKind::Abort
64             | TerminatorKind::Return
65             | TerminatorKind::Unreachable
66             | TerminatorKind::FalseEdge { .. }
67             | TerminatorKind::FalseUnwind { .. } => {
68                 // safe (at least as emitted during MIR construction)
69             }
70
71             TerminatorKind::Call { ref func, .. } => {
72                 let func_ty = func.ty(self.body, self.tcx);
73                 let func_id =
74                     if let ty::FnDef(func_id, _) = func_ty.kind() { Some(func_id) } else { None };
75                 let sig = func_ty.fn_sig(self.tcx);
76                 if let hir::Unsafety::Unsafe = sig.unsafety() {
77                     self.require_unsafe(
78                         UnsafetyViolationKind::General,
79                         UnsafetyViolationDetails::CallToUnsafeFunction,
80                     )
81                 }
82
83                 if let Some(func_id) = func_id {
84                     self.check_target_features(*func_id);
85                 }
86             }
87
88             TerminatorKind::InlineAsm { .. } => self.require_unsafe(
89                 UnsafetyViolationKind::General,
90                 UnsafetyViolationDetails::UseOfInlineAssembly,
91             ),
92         }
93         self.super_terminator(terminator, location);
94     }
95
96     fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
97         self.source_info = statement.source_info;
98         match statement.kind {
99             StatementKind::Assign(..)
100             | StatementKind::FakeRead(..)
101             | StatementKind::SetDiscriminant { .. }
102             | StatementKind::Deinit(..)
103             | StatementKind::StorageLive(..)
104             | StatementKind::StorageDead(..)
105             | StatementKind::Retag { .. }
106             | StatementKind::AscribeUserType(..)
107             | StatementKind::Coverage(..)
108             | StatementKind::Nop => {
109                 // safe (at least as emitted during MIR construction)
110             }
111
112             StatementKind::CopyNonOverlapping(..) => unreachable!(),
113         }
114         self.super_statement(statement, location);
115     }
116
117     fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
118         match rvalue {
119             Rvalue::Aggregate(box ref aggregate, _) => match aggregate {
120                 &AggregateKind::Array(..) | &AggregateKind::Tuple => {}
121                 &AggregateKind::Adt(adt_did, ..) => {
122                     match self.tcx.layout_scalar_valid_range(adt_did) {
123                         (Bound::Unbounded, Bound::Unbounded) => {}
124                         _ => self.require_unsafe(
125                             UnsafetyViolationKind::General,
126                             UnsafetyViolationDetails::InitializingTypeWith,
127                         ),
128                     }
129                 }
130                 &AggregateKind::Closure(def_id, _) | &AggregateKind::Generator(def_id, _, _) => {
131                     let UnsafetyCheckResult { violations, used_unsafe_blocks, .. } =
132                         self.tcx.unsafety_check_result(def_id.expect_local());
133                     self.register_violations(
134                         violations,
135                         used_unsafe_blocks.iter().map(|(&h, &d)| (h, d)),
136                     );
137                 }
138             },
139             _ => {}
140         }
141         self.super_rvalue(rvalue, location);
142     }
143
144     fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, _location: Location) {
145         // On types with `scalar_valid_range`, prevent
146         // * `&mut x.field`
147         // * `x.field = y;`
148         // * `&x.field` if `field`'s type has interior mutability
149         // because either of these would allow modifying the layout constrained field and
150         // insert values that violate the layout constraints.
151         if context.is_mutating_use() || context.is_borrow() {
152             self.check_mut_borrowing_layout_constrained_field(*place, context.is_mutating_use());
153         }
154
155         // Some checks below need the extra meta info of the local declaration.
156         let decl = &self.body.local_decls[place.local];
157
158         // Check the base local: it might be an unsafe-to-access static. We only check derefs of the
159         // temporary holding the static pointer to avoid duplicate errors
160         // <https://github.com/rust-lang/rust/pull/78068#issuecomment-731753506>.
161         if decl.internal && place.projection.first() == Some(&ProjectionElem::Deref) {
162             // If the projection root is an artificial local that we introduced when
163             // desugaring `static`, give a more specific error message
164             // (avoid the general "raw pointer" clause below, that would only be confusing).
165             if let Some(box LocalInfo::StaticRef { def_id, .. }) = decl.local_info {
166                 if self.tcx.is_mutable_static(def_id) {
167                     self.require_unsafe(
168                         UnsafetyViolationKind::General,
169                         UnsafetyViolationDetails::UseOfMutableStatic,
170                     );
171                     return;
172                 } else if self.tcx.is_foreign_item(def_id) {
173                     self.require_unsafe(
174                         UnsafetyViolationKind::General,
175                         UnsafetyViolationDetails::UseOfExternStatic,
176                     );
177                     return;
178                 }
179             }
180         }
181
182         // Check for raw pointer `Deref`.
183         for (base, proj) in place.iter_projections() {
184             if proj == ProjectionElem::Deref {
185                 let base_ty = base.ty(self.body, self.tcx).ty;
186                 if base_ty.is_unsafe_ptr() {
187                     self.require_unsafe(
188                         UnsafetyViolationKind::General,
189                         UnsafetyViolationDetails::DerefOfRawPointer,
190                     )
191                 }
192             }
193         }
194
195         // Check for union fields. For this we traverse right-to-left, as the last `Deref` changes
196         // whether we *read* the union field or potentially *write* to it (if this place is being assigned to).
197         let mut saw_deref = false;
198         for (base, proj) in place.iter_projections().rev() {
199             if proj == ProjectionElem::Deref {
200                 saw_deref = true;
201                 continue;
202             }
203
204             let base_ty = base.ty(self.body, self.tcx).ty;
205             if base_ty.is_union() {
206                 // If we did not hit a `Deref` yet and the overall place use is an assignment, the
207                 // rules are different.
208                 let assign_to_field = !saw_deref
209                     && matches!(
210                         context,
211                         PlaceContext::MutatingUse(
212                             MutatingUseContext::Store
213                                 | MutatingUseContext::Drop
214                                 | MutatingUseContext::AsmOutput
215                         )
216                     );
217                 // If this is just an assignment, determine if the assigned type needs dropping.
218                 if assign_to_field {
219                     // We have to check the actual type of the assignment, as that determines if the
220                     // old value is being dropped.
221                     let assigned_ty = place.ty(&self.body.local_decls, self.tcx).ty;
222                     if assigned_ty.needs_drop(
223                         self.tcx,
224                         self.tcx.param_env(base_ty.ty_adt_def().unwrap().did()),
225                     ) {
226                         // This would be unsafe, but should be outright impossible since we reject such unions.
227                         self.tcx.sess.delay_span_bug(
228                             self.source_info.span,
229                             "union fields that need dropping should be impossible",
230                         );
231                     }
232                 } else {
233                     self.require_unsafe(
234                         UnsafetyViolationKind::General,
235                         UnsafetyViolationDetails::AccessToUnionField,
236                     )
237                 }
238             }
239         }
240     }
241 }
242
243 impl<'tcx> UnsafetyChecker<'_, 'tcx> {
244     fn require_unsafe(&mut self, kind: UnsafetyViolationKind, details: UnsafetyViolationDetails) {
245         // Violations can turn out to be `UnsafeFn` during analysis, but they should not start out as such.
246         assert_ne!(kind, UnsafetyViolationKind::UnsafeFn);
247
248         let source_info = self.source_info;
249         let lint_root = self.body.source_scopes[self.source_info.scope]
250             .local_data
251             .as_ref()
252             .assert_crate_local()
253             .lint_root;
254         self.register_violations(
255             [&UnsafetyViolation { source_info, lint_root, kind, details }],
256             [],
257         );
258     }
259
260     fn register_violations<'a>(
261         &mut self,
262         violations: impl IntoIterator<Item = &'a UnsafetyViolation>,
263         new_used_unsafe_blocks: impl IntoIterator<Item = (HirId, UsedUnsafeBlockData)>,
264     ) {
265         use UsedUnsafeBlockData::{AllAllowedInUnsafeFn, SomeDisallowedInUnsafeFn};
266
267         let update_entry = |this: &mut Self, hir_id, new_usage| {
268             match this.used_unsafe_blocks.entry(hir_id) {
269                 hash_map::Entry::Occupied(mut entry) => {
270                     if new_usage == SomeDisallowedInUnsafeFn {
271                         *entry.get_mut() = SomeDisallowedInUnsafeFn;
272                     }
273                 }
274                 hash_map::Entry::Vacant(entry) => {
275                     entry.insert(new_usage);
276                 }
277             };
278         };
279         let safety = self.body.source_scopes[self.source_info.scope]
280             .local_data
281             .as_ref()
282             .assert_crate_local()
283             .safety;
284         match safety {
285             // `unsafe` blocks are required in safe code
286             Safety::Safe => violations.into_iter().for_each(|&violation| {
287                 match violation.kind {
288                     UnsafetyViolationKind::General => {}
289                     UnsafetyViolationKind::UnsafeFn => {
290                         bug!("`UnsafetyViolationKind::UnsafeFn` in an `Safe` context")
291                     }
292                 }
293                 if !self.violations.contains(&violation) {
294                     self.violations.push(violation)
295                 }
296             }),
297             // With the RFC 2585, no longer allow `unsafe` operations in `unsafe fn`s
298             Safety::FnUnsafe => violations.into_iter().for_each(|&(mut violation)| {
299                 violation.kind = UnsafetyViolationKind::UnsafeFn;
300                 if !self.violations.contains(&violation) {
301                     self.violations.push(violation)
302                 }
303             }),
304             Safety::BuiltinUnsafe => {}
305             Safety::ExplicitUnsafe(hir_id) => violations.into_iter().for_each(|violation| {
306                 update_entry(
307                     self,
308                     hir_id,
309                     match self.tcx.lint_level_at_node(UNSAFE_OP_IN_UNSAFE_FN, violation.lint_root).0
310                     {
311                         Level::Allow => AllAllowedInUnsafeFn(violation.lint_root),
312                         _ => SomeDisallowedInUnsafeFn,
313                     },
314                 )
315             }),
316         };
317
318         new_used_unsafe_blocks
319             .into_iter()
320             .for_each(|(hir_id, usage_data)| update_entry(self, hir_id, usage_data));
321     }
322     fn check_mut_borrowing_layout_constrained_field(
323         &mut self,
324         place: Place<'tcx>,
325         is_mut_use: bool,
326     ) {
327         for (place_base, elem) in place.iter_projections().rev() {
328             match elem {
329                 // Modifications behind a dereference don't affect the value of
330                 // the pointer.
331                 ProjectionElem::Deref => return,
332                 ProjectionElem::Field(..) => {
333                     let ty = place_base.ty(&self.body.local_decls, self.tcx).ty;
334                     if let ty::Adt(def, _) = ty.kind() {
335                         if self.tcx.layout_scalar_valid_range(def.did())
336                             != (Bound::Unbounded, Bound::Unbounded)
337                         {
338                             let details = if is_mut_use {
339                                 UnsafetyViolationDetails::MutationOfLayoutConstrainedField
340
341                             // Check `is_freeze` as late as possible to avoid cycle errors
342                             // with opaque types.
343                             } else if !place
344                                 .ty(self.body, self.tcx)
345                                 .ty
346                                 .is_freeze(self.tcx.at(self.source_info.span), self.param_env)
347                             {
348                                 UnsafetyViolationDetails::BorrowOfLayoutConstrainedField
349                             } else {
350                                 continue;
351                             };
352                             self.require_unsafe(UnsafetyViolationKind::General, details);
353                         }
354                     }
355                 }
356                 _ => {}
357             }
358         }
359     }
360
361     /// Checks whether calling `func_did` needs an `unsafe` context or not, i.e. whether
362     /// the called function has target features the calling function hasn't.
363     fn check_target_features(&mut self, func_did: DefId) {
364         // Unsafety isn't required on wasm targets. For more information see
365         // the corresponding check in typeck/src/collect.rs
366         if self.tcx.sess.target.options.is_like_wasm {
367             return;
368         }
369
370         let callee_features = &self.tcx.codegen_fn_attrs(func_did).target_features;
371         // The body might be a constant, so it doesn't have codegen attributes.
372         let self_features = &self.tcx.body_codegen_attrs(self.body_did.to_def_id()).target_features;
373
374         // Is `callee_features` a subset of `calling_features`?
375         if !callee_features.iter().all(|feature| self_features.contains(feature)) {
376             self.require_unsafe(
377                 UnsafetyViolationKind::General,
378                 UnsafetyViolationDetails::CallToFunctionWith,
379             )
380         }
381     }
382 }
383
384 pub(crate) fn provide(providers: &mut Providers) {
385     *providers = Providers {
386         unsafety_check_result: |tcx, def_id| {
387             if let Some(def) = ty::WithOptConstParam::try_lookup(def_id, tcx) {
388                 tcx.unsafety_check_result_for_const_arg(def)
389             } else {
390                 unsafety_check_result(tcx, ty::WithOptConstParam::unknown(def_id))
391             }
392         },
393         unsafety_check_result_for_const_arg: |tcx, (did, param_did)| {
394             unsafety_check_result(
395                 tcx,
396                 ty::WithOptConstParam { did, const_param_did: Some(param_did) },
397             )
398         },
399         ..*providers
400     };
401 }
402
403 /// Context information for [`UnusedUnsafeVisitor`] traversal,
404 /// saves (innermost) relevant context
405 #[derive(Copy, Clone, Debug)]
406 enum Context {
407     Safe,
408     /// in an `unsafe fn`
409     UnsafeFn(HirId),
410     /// in a *used* `unsafe` block
411     /// (i.e. a block without unused-unsafe warning)
412     UnsafeBlock(HirId),
413 }
414
415 struct UnusedUnsafeVisitor<'a, 'tcx> {
416     tcx: TyCtxt<'tcx>,
417     used_unsafe_blocks: &'a FxHashMap<HirId, UsedUnsafeBlockData>,
418     context: Context,
419     unused_unsafes: &'a mut Vec<(HirId, UnusedUnsafe)>,
420 }
421
422 impl<'tcx> intravisit::Visitor<'tcx> for UnusedUnsafeVisitor<'_, 'tcx> {
423     fn visit_block(&mut self, block: &'tcx hir::Block<'tcx>) {
424         use UsedUnsafeBlockData::{AllAllowedInUnsafeFn, SomeDisallowedInUnsafeFn};
425
426         if let hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::UserProvided) = block.rules {
427             let used = match self.tcx.lint_level_at_node(UNUSED_UNSAFE, block.hir_id) {
428                 (Level::Allow, _) => Some(SomeDisallowedInUnsafeFn),
429                 _ => self.used_unsafe_blocks.get(&block.hir_id).copied(),
430             };
431             let unused_unsafe = match (self.context, used) {
432                 (_, None) => UnusedUnsafe::Unused,
433                 (Context::Safe, Some(_))
434                 | (Context::UnsafeFn(_), Some(SomeDisallowedInUnsafeFn)) => {
435                     let previous_context = self.context;
436                     self.context = Context::UnsafeBlock(block.hir_id);
437                     intravisit::walk_block(self, block);
438                     self.context = previous_context;
439                     return;
440                 }
441                 (Context::UnsafeFn(hir_id), Some(AllAllowedInUnsafeFn(lint_root))) => {
442                     UnusedUnsafe::InUnsafeFn(hir_id, lint_root)
443                 }
444                 (Context::UnsafeBlock(hir_id), Some(_)) => UnusedUnsafe::InUnsafeBlock(hir_id),
445             };
446             self.unused_unsafes.push((block.hir_id, unused_unsafe));
447         }
448         intravisit::walk_block(self, block);
449     }
450
451     fn visit_fn(
452         &mut self,
453         fk: intravisit::FnKind<'tcx>,
454         _fd: &'tcx hir::FnDecl<'tcx>,
455         b: hir::BodyId,
456         _s: rustc_span::Span,
457         _id: HirId,
458     ) {
459         if matches!(fk, intravisit::FnKind::Closure) {
460             self.visit_body(self.tcx.hir().body(b))
461         }
462     }
463 }
464
465 fn check_unused_unsafe(
466     tcx: TyCtxt<'_>,
467     def_id: LocalDefId,
468     used_unsafe_blocks: &FxHashMap<HirId, UsedUnsafeBlockData>,
469 ) -> Vec<(HirId, UnusedUnsafe)> {
470     let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
471     let body_id = tcx.hir().maybe_body_owned_by(hir_id);
472
473     let Some(body_id) = body_id else {
474         debug!("check_unused_unsafe({:?}) - no body found", def_id);
475         return vec![];
476     };
477     let body = tcx.hir().body(body_id);
478
479     let context = match tcx.hir().fn_sig_by_hir_id(hir_id) {
480         Some(sig) if sig.header.unsafety == hir::Unsafety::Unsafe => Context::UnsafeFn(hir_id),
481         _ => Context::Safe,
482     };
483
484     debug!(
485         "check_unused_unsafe({:?}, context={:?}, body={:?}, used_unsafe_blocks={:?})",
486         def_id, body, context, used_unsafe_blocks
487     );
488
489     let mut unused_unsafes = vec![];
490
491     let mut visitor = UnusedUnsafeVisitor {
492         tcx,
493         used_unsafe_blocks,
494         context,
495         unused_unsafes: &mut unused_unsafes,
496     };
497     intravisit::Visitor::visit_body(&mut visitor, body);
498
499     unused_unsafes
500 }
501
502 fn unsafety_check_result<'tcx>(
503     tcx: TyCtxt<'tcx>,
504     def: ty::WithOptConstParam<LocalDefId>,
505 ) -> &'tcx UnsafetyCheckResult {
506     debug!("unsafety_violations({:?})", def);
507
508     // N.B., this borrow is valid because all the consumers of
509     // `mir_built` force this.
510     let body = &tcx.mir_built(def).borrow();
511
512     let param_env = tcx.param_env(def.did);
513
514     let mut checker = UnsafetyChecker::new(body, def.did, tcx, param_env);
515     checker.visit_body(&body);
516
517     let unused_unsafes = (!tcx.is_closure(def.did.to_def_id()))
518         .then(|| check_unused_unsafe(tcx, def.did, &checker.used_unsafe_blocks));
519
520     tcx.arena.alloc(UnsafetyCheckResult {
521         violations: checker.violations,
522         used_unsafe_blocks: checker.used_unsafe_blocks,
523         unused_unsafes,
524     })
525 }
526
527 fn report_unused_unsafe(tcx: TyCtxt<'_>, kind: UnusedUnsafe, id: HirId) {
528     let span = tcx.sess.source_map().guess_head_span(tcx.hir().span(id));
529     tcx.struct_span_lint_hir(UNUSED_UNSAFE, id, span, |lint| {
530         let msg = "unnecessary `unsafe` block";
531         let mut db = lint.build(msg);
532         db.span_label(span, msg);
533         match kind {
534             UnusedUnsafe::Unused => {}
535             UnusedUnsafe::InUnsafeBlock(id) => {
536                 db.span_label(
537                     tcx.sess.source_map().guess_head_span(tcx.hir().span(id)),
538                     "because it's nested under this `unsafe` block",
539                 );
540             }
541             UnusedUnsafe::InUnsafeFn(id, usage_lint_root) => {
542                 db.span_label(
543                     tcx.sess.source_map().guess_head_span(tcx.hir().span(id)),
544                     "because it's nested under this `unsafe` fn",
545                 )
546                 .note(
547                     "this `unsafe` block does contain unsafe operations, \
548                     but those are already allowed in an `unsafe fn`",
549                 );
550                 let (level, source) =
551                     tcx.lint_level_at_node(UNSAFE_OP_IN_UNSAFE_FN, usage_lint_root);
552                 assert_eq!(level, Level::Allow);
553                 lint::explain_lint_level_source(
554                     UNSAFE_OP_IN_UNSAFE_FN,
555                     Level::Allow,
556                     source,
557                     &mut db,
558                 );
559             }
560         }
561
562         db.emit();
563     });
564 }
565
566 pub fn check_unsafety(tcx: TyCtxt<'_>, def_id: LocalDefId) {
567     debug!("check_unsafety({:?})", def_id);
568
569     // closures are handled by their parent fn.
570     if tcx.is_closure(def_id.to_def_id()) {
571         return;
572     }
573
574     let UnsafetyCheckResult { violations, unused_unsafes, .. } = tcx.unsafety_check_result(def_id);
575
576     for &UnsafetyViolation { source_info, lint_root, kind, details } in violations.iter() {
577         let (description, note) = details.description_and_note();
578
579         // Report an error.
580         let unsafe_fn_msg =
581             if unsafe_op_in_unsafe_fn_allowed(tcx, lint_root) { " function or" } else { "" };
582
583         match kind {
584             UnsafetyViolationKind::General => {
585                 // once
586                 struct_span_err!(
587                     tcx.sess,
588                     source_info.span,
589                     E0133,
590                     "{} is unsafe and requires unsafe{} block",
591                     description,
592                     unsafe_fn_msg,
593                 )
594                 .span_label(source_info.span, description)
595                 .note(note)
596                 .emit();
597             }
598             UnsafetyViolationKind::UnsafeFn => tcx.struct_span_lint_hir(
599                 UNSAFE_OP_IN_UNSAFE_FN,
600                 lint_root,
601                 source_info.span,
602                 |lint| {
603                     lint.build(&format!(
604                         "{} is unsafe and requires unsafe block (error E0133)",
605                         description,
606                     ))
607                     .span_label(source_info.span, description)
608                     .note(note)
609                     .emit();
610                 },
611             ),
612         }
613     }
614
615     for &(block_id, kind) in unused_unsafes.as_ref().unwrap() {
616         report_unused_unsafe(tcx, kind, block_id);
617     }
618 }
619
620 fn unsafe_op_in_unsafe_fn_allowed(tcx: TyCtxt<'_>, id: HirId) -> bool {
621     tcx.lint_level_at_node(UNSAFE_OP_IN_UNSAFE_FN, id).0 == Level::Allow
622 }