|
| 1 | +use rustc_hir as hir; |
| 2 | +use rustc_hir_pretty::qpath_to_string; |
| 3 | +use rustc_lint_defs::builtin::STATIC_MUT_REF; |
| 4 | +use rustc_middle::ty::TyCtxt; |
| 5 | +use rustc_span::Span; |
| 6 | +use rustc_type_ir::Mutability; |
| 7 | + |
| 8 | +use crate::errors; |
| 9 | + |
| 10 | +/// Check for shared or mutable references of `static mut` inside expression |
| 11 | +pub fn maybe_expr_static_mut(tcx: TyCtxt<'_>, expr: hir::Expr<'_>) { |
| 12 | + let span = expr.span; |
| 13 | + let hir_id = expr.hir_id; |
| 14 | + if let hir::ExprKind::AddrOf(borrow_kind, m, expr) = expr.kind |
| 15 | + && matches!(borrow_kind, hir::BorrowKind::Ref) |
| 16 | + && let Some(var) = is_path_static_mut(*expr) |
| 17 | + { |
| 18 | + handle_static_mut_ref( |
| 19 | + tcx, |
| 20 | + span, |
| 21 | + var, |
| 22 | + span.edition().at_least_rust_2024(), |
| 23 | + matches!(m, Mutability::Mut), |
| 24 | + hir_id, |
| 25 | + ); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +/// Check for shared or mutable references of `static mut` inside statement |
| 30 | +pub fn maybe_stmt_static_mut(tcx: TyCtxt<'_>, stmt: hir::Stmt<'_>) { |
| 31 | + if let hir::StmtKind::Local(loc) = stmt.kind |
| 32 | + && let hir::PatKind::Binding(ba, _, _, _) = loc.pat.kind |
| 33 | + && matches!(ba.0, rustc_ast::ByRef::Yes) |
| 34 | + && let Some(init) = loc.init |
| 35 | + && let Some(var) = is_path_static_mut(*init) |
| 36 | + { |
| 37 | + handle_static_mut_ref( |
| 38 | + tcx, |
| 39 | + init.span, |
| 40 | + var, |
| 41 | + loc.span.edition().at_least_rust_2024(), |
| 42 | + matches!(ba.1, Mutability::Mut), |
| 43 | + stmt.hir_id, |
| 44 | + ); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +fn is_path_static_mut(expr: hir::Expr<'_>) -> Option<String> { |
| 49 | + if let hir::ExprKind::Path(qpath) = expr.kind |
| 50 | + && let hir::QPath::Resolved(_, path) = qpath |
| 51 | + && let hir::def::Res::Def(def_kind, _) = path.res |
| 52 | + && let hir::def::DefKind::Static(mt) = def_kind |
| 53 | + && matches!(mt, Mutability::Mut) |
| 54 | + { |
| 55 | + return Some(qpath_to_string(&qpath)); |
| 56 | + } |
| 57 | + None |
| 58 | +} |
| 59 | + |
| 60 | +fn handle_static_mut_ref( |
| 61 | + tcx: TyCtxt<'_>, |
| 62 | + span: Span, |
| 63 | + var: String, |
| 64 | + e2024: bool, |
| 65 | + mutable: bool, |
| 66 | + hir_id: hir::HirId, |
| 67 | +) { |
| 68 | + if e2024 { |
| 69 | + let sugg = if mutable { |
| 70 | + errors::StaticMutRefSugg::Mut { span, var } |
| 71 | + } else { |
| 72 | + errors::StaticMutRefSugg::Shared { span, var } |
| 73 | + }; |
| 74 | + tcx.sess.parse_sess.dcx.emit_err(errors::StaticMutRef { span, sugg }); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + let (label, sugg, shared) = if mutable { |
| 79 | + ( |
| 80 | + errors::RefOfMutStaticLabel::Mut { span }, |
| 81 | + errors::RefOfMutStaticSugg::Mut { span, var }, |
| 82 | + "mutable ", |
| 83 | + ) |
| 84 | + } else { |
| 85 | + ( |
| 86 | + errors::RefOfMutStaticLabel::Shared { span }, |
| 87 | + errors::RefOfMutStaticSugg::Shared { span, var }, |
| 88 | + "shared ", |
| 89 | + ) |
| 90 | + }; |
| 91 | + tcx.emit_spanned_lint( |
| 92 | + STATIC_MUT_REF, |
| 93 | + hir_id, |
| 94 | + span, |
| 95 | + errors::RefOfMutStatic { shared, why_note: (), label, sugg }, |
| 96 | + ); |
| 97 | +} |
0 commit comments