|
| 1 | +use clippy_utils::{diagnostics::span_lint_and_sugg, in_macro, is_lint_allowed, source::snippet_with_applicability}; |
| 2 | +use rustc_errors::Applicability; |
| 3 | +use rustc_hir::{intravisit::FnKind, Block, BlockCheckMode, Body, FnDecl, FnSig, HirId, UnsafeSource, Unsafety}; |
| 4 | +use rustc_lint::{ |
| 5 | + builtin::{UNSAFE_OP_IN_UNSAFE_FN, UNUSED_UNSAFE}, |
| 6 | + LateContext, LateLintPass, LintContext, |
| 7 | +}; |
| 8 | +use rustc_middle::lint::in_external_macro; |
| 9 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 10 | +use rustc_span::Span; |
| 11 | + |
| 12 | +declare_clippy_lint! { |
| 13 | + /// **What it does:** Checks for macro calls inside an `unsafe` function which expansion |
| 14 | + /// contains an `unsafe` block, while the macro call is not wrapped in an `unsafe` block |
| 15 | + /// itself. This lint only triggers in functions where the `unsafe_op_in_unsafe_fn` lint is |
| 16 | + /// enabled. |
| 17 | + /// |
| 18 | + /// **Why is this bad?** This hides an unsafe operation inside a macro call. This is against |
| 19 | + /// the intention of the `unsafe_op_in_unsafe_fn` lint, which is meant to make unsafe code more |
| 20 | + /// visible by requiring `unsafe` blocks also in `unsafe` functions. |
| 21 | + /// |
| 22 | + /// **Known problems:** |
| 23 | + /// - In some cases the intention of the macro is to actually hide the unsafety, because the |
| 24 | + /// macro itself ensures the safety of the `unsafe` operation. |
| 25 | + /// - For local macros, either an `#[allow(unused_unsafe)]` has to be added to the new unsafe |
| 26 | + /// block or the unsafe block inside the macro has to be removed. |
| 27 | + /// |
| 28 | + /// **Example:** |
| 29 | + /// |
| 30 | + /// ```rust |
| 31 | + /// macro_rules! unsafe_macro { |
| 32 | + /// ($e:expr) => { |
| 33 | + /// unsafe { $e }; |
| 34 | + /// }; |
| 35 | + /// } |
| 36 | + /// |
| 37 | + /// #[warn(unsafe_op_in_unsafe_fn)] |
| 38 | + /// unsafe fn foo(x: *const usize) { |
| 39 | + /// unsafe_macro!(std::ptr::read(x)); |
| 40 | + /// } |
| 41 | + /// ``` |
| 42 | + /// Use instead: |
| 43 | + /// ```rust |
| 44 | + /// # macro_rules! unsafe_macro { |
| 45 | + /// # ($e:expr) => { |
| 46 | + /// # unsafe { $e }; |
| 47 | + /// # }; |
| 48 | + /// # } |
| 49 | + /// #[warn(unsafe_op_in_unsafe_fn)] |
| 50 | + /// unsafe fn foo(x: *const usize) { |
| 51 | + /// #[allow(unused_unsafe)] unsafe { unsafe_macro!(std::ptr::read(x)) }; |
| 52 | + /// } |
| 53 | + /// ``` |
| 54 | + pub MACROS_HIDING_UNSAFE_CODE, |
| 55 | + restriction, |
| 56 | + "macros hiding unsafe code, while `unsafe_op_in_unsafe_fn` is enabled" |
| 57 | +} |
| 58 | + |
| 59 | +#[derive(Clone, Copy, Default)] |
| 60 | +pub struct MacrosHidingUnsafeCode { |
| 61 | + in_unsafe_fn: bool, |
| 62 | + in_unsafe_block: usize, |
| 63 | +} |
| 64 | + |
| 65 | +impl_lint_pass!(MacrosHidingUnsafeCode => [MACROS_HIDING_UNSAFE_CODE]); |
| 66 | + |
| 67 | +impl LateLintPass<'_> for MacrosHidingUnsafeCode { |
| 68 | + fn check_block(&mut self, cx: &LateContext<'_>, block: &Block<'_>) { |
| 69 | + if let BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided) = block.rules { |
| 70 | + if in_macro(block.span) { |
| 71 | + if self.in_unsafe_fn |
| 72 | + && self.in_unsafe_block == 0 |
| 73 | + && !is_lint_allowed(cx, UNSAFE_OP_IN_UNSAFE_FN, block.hir_id) |
| 74 | + { |
| 75 | + let macro_call_span = block.span.source_callsite(); |
| 76 | + let unused_unsafe_sugg = if !in_external_macro(cx.sess(), block.span) |
| 77 | + && !is_lint_allowed(cx, UNUSED_UNSAFE, block.hir_id) |
| 78 | + { |
| 79 | + "#[allow(unused_unsafe)] " |
| 80 | + } else { |
| 81 | + "" |
| 82 | + }; |
| 83 | + let mut applicability = Applicability::MaybeIncorrect; |
| 84 | + span_lint_and_sugg( |
| 85 | + cx, |
| 86 | + MACROS_HIDING_UNSAFE_CODE, |
| 87 | + macro_call_span, |
| 88 | + "this macro call hides unsafe code", |
| 89 | + "wrap it in an `unsafe` block", |
| 90 | + format!( |
| 91 | + "{}unsafe {{ {} }}", |
| 92 | + unused_unsafe_sugg, |
| 93 | + snippet_with_applicability(cx, macro_call_span, "...", &mut applicability), |
| 94 | + ), |
| 95 | + applicability, |
| 96 | + ); |
| 97 | + } |
| 98 | + } else { |
| 99 | + self.in_unsafe_block = self.in_unsafe_block.saturating_add(1); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + fn check_block_post(&mut self, _: &LateContext<'_>, block: &Block<'_>) { |
| 105 | + if let BlockCheckMode::UnsafeBlock(UnsafeSource::UserProvided) = block.rules { |
| 106 | + if !in_macro(block.span) { |
| 107 | + self.in_unsafe_block = self.in_unsafe_block.saturating_sub(1); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + fn check_fn(&mut self, _: &LateContext<'_>, fn_kind: FnKind<'_>, _: &FnDecl<'_>, _: &Body<'_>, _: Span, _: HirId) { |
| 113 | + if is_unsafe_fn(fn_kind) { |
| 114 | + self.in_unsafe_fn = true; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + fn check_fn_post( |
| 119 | + &mut self, |
| 120 | + _: &LateContext<'_>, |
| 121 | + fn_kind: FnKind<'_>, |
| 122 | + _: &FnDecl<'_>, |
| 123 | + _: &Body<'_>, |
| 124 | + _: Span, |
| 125 | + _: HirId, |
| 126 | + ) { |
| 127 | + if is_unsafe_fn(fn_kind) { |
| 128 | + self.in_unsafe_fn = false; |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +fn is_unsafe_fn(fn_kind: FnKind<'_>) -> bool { |
| 134 | + match fn_kind { |
| 135 | + FnKind::ItemFn(_, _, header, _) | FnKind::Method(_, &FnSig { header, .. }, _) => { |
| 136 | + matches!(header.unsafety, Unsafety::Unsafe) |
| 137 | + }, |
| 138 | + FnKind::Closure => false, |
| 139 | + } |
| 140 | +} |
0 commit comments