|
| 1 | +use rustc_hir as hir; |
| 2 | +use rustc_hir::{Expr, Stmt}; |
| 3 | +use rustc_middle::ty::{Mutability, TyKind}; |
| 4 | +use rustc_session::lint::FutureIncompatibilityReason; |
| 5 | +use rustc_session::{declare_lint, declare_lint_pass}; |
| 6 | +use rustc_span::edition::Edition; |
| 7 | +use rustc_span::Span; |
| 8 | + |
| 9 | +use crate::lints::{MutRefSugg, RefOfMutStatic}; |
| 10 | +use crate::{LateContext, LateLintPass, LintContext}; |
| 11 | + |
| 12 | +declare_lint! { |
| 13 | + /// The `static_mut_refs` lint checks for shared or mutable references |
| 14 | + /// of mutable static inside `unsafe` blocks and `unsafe` functions. |
| 15 | + /// |
| 16 | + /// ### Example |
| 17 | + /// |
| 18 | + /// ```rust,edition2021 |
| 19 | + /// fn main() { |
| 20 | + /// static mut X: i32 = 23; |
| 21 | + /// static mut Y: i32 = 24; |
| 22 | + /// |
| 23 | + /// unsafe { |
| 24 | + /// let y = &X; |
| 25 | + /// let ref x = X; |
| 26 | + /// let (x, y) = (&X, &Y); |
| 27 | + /// foo(&X); |
| 28 | + /// } |
| 29 | + /// } |
| 30 | + /// |
| 31 | + /// unsafe fn _foo() { |
| 32 | + /// static mut X: i32 = 23; |
| 33 | + /// static mut Y: i32 = 24; |
| 34 | + /// |
| 35 | + /// let y = &X; |
| 36 | + /// let ref x = X; |
| 37 | + /// let (x, y) = (&X, &Y); |
| 38 | + /// foo(&X); |
| 39 | + /// } |
| 40 | + /// |
| 41 | + /// fn foo<'a>(_x: &'a i32) {} |
| 42 | + /// ``` |
| 43 | + /// |
| 44 | + /// {{produces}} |
| 45 | + /// |
| 46 | + /// ### Explanation |
| 47 | + /// |
| 48 | + /// Shared or mutable references of mutable static are almost always a mistake and |
| 49 | + /// can lead to undefined behavior and various other problems in your code. |
| 50 | + /// |
| 51 | + /// This lint is "warn" by default on editions up to 2021, in 2024 is "deny". |
| 52 | + pub STATIC_MUT_REFS, |
| 53 | + Warn, |
| 54 | + "shared references or mutable references of mutable static is discouraged", |
| 55 | + @future_incompatible = FutureIncompatibleInfo { |
| 56 | + reason: FutureIncompatibilityReason::EditionError(Edition::Edition2024), |
| 57 | + reference: "<https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>", |
| 58 | + explain_reason: false, |
| 59 | + }; |
| 60 | + @edition Edition2024 => Deny; |
| 61 | +} |
| 62 | + |
| 63 | +declare_lint_pass!(StaticMutRefs => [STATIC_MUT_REFS]); |
| 64 | + |
| 65 | +impl<'tcx> LateLintPass<'tcx> for StaticMutRefs { |
| 66 | + #[allow(rustc::usage_of_ty_tykind)] |
| 67 | + fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'_>) { |
| 68 | + let err_span = expr.span; |
| 69 | + match expr.kind { |
| 70 | + hir::ExprKind::AddrOf(borrow_kind, m, ex) |
| 71 | + if matches!(borrow_kind, hir::BorrowKind::Ref) |
| 72 | + && let Some(err_span) = path_is_static_mut(ex, err_span) => |
| 73 | + { |
| 74 | + emit_static_mut_refs( |
| 75 | + cx, |
| 76 | + err_span, |
| 77 | + err_span.with_hi(ex.span.lo()), |
| 78 | + m, |
| 79 | + !expr.span.from_expansion(), |
| 80 | + ); |
| 81 | + } |
| 82 | + hir::ExprKind::MethodCall(_, e, _, _) |
| 83 | + if let Some(err_span) = path_is_static_mut(e, expr.span) |
| 84 | + && let typeck = cx.typeck_results() |
| 85 | + && let Some(method_def_id) = typeck.type_dependent_def_id(expr.hir_id) |
| 86 | + && let inputs = |
| 87 | + cx.tcx.fn_sig(method_def_id).skip_binder().inputs().skip_binder() |
| 88 | + && let Some(receiver) = inputs.get(0) |
| 89 | + && let TyKind::Ref(_, _, m) = receiver.kind() => |
| 90 | + { |
| 91 | + emit_static_mut_refs(cx, err_span, err_span.shrink_to_lo(), *m, false); |
| 92 | + } |
| 93 | + _ => {} |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &Stmt<'_>) { |
| 98 | + if let hir::StmtKind::Let(loc) = stmt.kind |
| 99 | + && let hir::PatKind::Binding(ba, _, _, _) = loc.pat.kind |
| 100 | + && let hir::ByRef::Yes(m) = ba.0 |
| 101 | + && let Some(init) = loc.init |
| 102 | + && let Some(err_span) = path_is_static_mut(init, init.span) |
| 103 | + { |
| 104 | + emit_static_mut_refs(cx, err_span, err_span.shrink_to_lo(), m, false); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +fn path_is_static_mut(mut expr: &hir::Expr<'_>, mut err_span: Span) -> Option<Span> { |
| 110 | + if err_span.from_expansion() { |
| 111 | + err_span = expr.span; |
| 112 | + } |
| 113 | + |
| 114 | + while let hir::ExprKind::Field(e, _) = expr.kind { |
| 115 | + expr = e; |
| 116 | + } |
| 117 | + |
| 118 | + if let hir::ExprKind::Path(qpath) = expr.kind |
| 119 | + && let hir::QPath::Resolved(_, path) = qpath |
| 120 | + && let hir::def::Res::Def(def_kind, _) = path.res |
| 121 | + && let hir::def::DefKind::Static { safety: _, mutability: Mutability::Mut, nested: false } = |
| 122 | + def_kind |
| 123 | + { |
| 124 | + return Some(err_span); |
| 125 | + } |
| 126 | + None |
| 127 | +} |
| 128 | + |
| 129 | +fn emit_static_mut_refs( |
| 130 | + cx: &LateContext<'_>, |
| 131 | + span: Span, |
| 132 | + sugg_span: Span, |
| 133 | + mutable: Mutability, |
| 134 | + suggest_addr_of: bool, |
| 135 | +) { |
| 136 | + let (shared_label, shared_note, mut_note, sugg) = match mutable { |
| 137 | + Mutability::Mut => { |
| 138 | + let sugg = |
| 139 | + if suggest_addr_of { Some(MutRefSugg::Mut { span: sugg_span }) } else { None }; |
| 140 | + ("mutable ", false, true, sugg) |
| 141 | + } |
| 142 | + Mutability::Not => { |
| 143 | + let sugg = |
| 144 | + if suggest_addr_of { Some(MutRefSugg::Shared { span: sugg_span }) } else { None }; |
| 145 | + ("shared ", true, false, sugg) |
| 146 | + } |
| 147 | + }; |
| 148 | + |
| 149 | + cx.emit_span_lint( |
| 150 | + STATIC_MUT_REFS, |
| 151 | + span, |
| 152 | + RefOfMutStatic { span, sugg, shared_label, shared_note, mut_note }, |
| 153 | + ); |
| 154 | +} |
0 commit comments