Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 70ba4d1

Browse files
committed
Disallow reference to static mut for statements
1 parent 2c088f9 commit 70ba4d1

File tree

1 file changed

+19
-0
lines changed
  • compiler/rustc_hir_analysis/src/check

1 file changed

+19
-0
lines changed

compiler/rustc_hir_analysis/src/check/errs.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,25 @@ pub fn maybe_expr_static_mut(tcx: TyCtxt<'_>, expr: hir::Expr<'_>) {
2626
}
2727
}
2828

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+
2948
fn is_path_static_mut(expr: hir::Expr<'_>) -> Option<String> {
3049
if let hir::ExprKind::Path(qpath) = expr.kind
3150
&& let hir::QPath::Resolved(_, path) = qpath

0 commit comments

Comments
 (0)