-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use crate::lints::UseOfStaticMut; | ||
use crate::{LateContext, LateLintPass, LintContext}; | ||
use rustc_ast::BorrowKind; | ||
use rustc_hir::{ | ||
def::{DefKind, Res::Def}, | ||
intravisit::FnKind, | ||
Block, BlockCheckMode, Body, ExprKind, FnDecl, QPath, Stmt, StmtKind, | ||
UnsafeSource::UserProvided, | ||
Unsafety, | ||
}; | ||
use rustc_span::{def_id::LocalDefId, Span}; | ||
use rustc_type_ir::Mutability; | ||
|
||
declare_lint! { | ||
/// The `static_mut_ref` lint checks for use of mutable static | ||
/// inside `unsafe` blocks and `unsafe` functions. | ||
/// | ||
/// ### Example | ||
/// | ||
/// ```rust,no_run,edition2024 | ||
/// fn main() { | ||
/// static mut X: i32 = 23; | ||
/// unsafe { | ||
/// let y = &X; | ||
/// } | ||
/// } | ||
/// | ||
/// unsafe fn foo() { | ||
/// static mut X: i32 = 23; | ||
/// let y = &X; | ||
/// } | ||
/// ``` | ||
/// | ||
/// {{produces}} | ||
/// | ||
/// ### Explanation | ||
/// | ||
/// Use of mutable static is almost always a mistake and can lead to | ||
/// undefined behavior and various other problems in your code. | ||
/// | ||
/// This lint is "warn" by default on editions up to 2021, from 2024 it is | ||
/// a hard error. | ||
pub STATIC_MUT_REF, | ||
Warn, | ||
"use of mutable static is discouraged" | ||
} | ||
|
||
declare_lint_pass!(StaticMutRef => [STATIC_MUT_REF]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for StaticMutRef { | ||
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) { | ||
if let BlockCheckMode::UnsafeBlock(src) = block.rules | ||
&& matches!(src, UserProvided) | ||
{ | ||
check_stmts(cx, block.stmts); | ||
} | ||
} | ||
|
||
fn check_fn( | ||
&mut self, | ||
cx: &LateContext<'tcx>, | ||
fn_kind: FnKind<'tcx>, | ||
_: &'tcx FnDecl<'tcx>, | ||
body: &'tcx Body<'tcx>, | ||
_: Span, | ||
_: LocalDefId, | ||
) { | ||
if let FnKind::ItemFn(_, _, h) = fn_kind | ||
&& matches!(h.unsafety, Unsafety::Unsafe) | ||
&& let ExprKind::Block(block, _) = body.value.kind | ||
{ | ||
check_stmts(cx, block.stmts); | ||
} | ||
} | ||
} | ||
|
||
fn check_stmts(cx: &LateContext<'_>, stmts: &[Stmt<'_>]) { | ||
for stmt in stmts.iter() { | ||
if let StmtKind::Local(loc) = stmt.kind | ||
&& let Some(init) = loc.init | ||
&& let ExprKind::AddrOf(borrow_kind, _, expr) = init.kind | ||
&& matches!(borrow_kind, BorrowKind::Ref) | ||
&& let ExprKind::Path(qpath) = expr.kind | ||
&& let QPath::Resolved(_, path) = qpath | ||
&& let Def(def_kind, _) = path.res | ||
&& let DefKind::Static(mt) = def_kind | ||
&& matches!(mt, Mutability::Mut) | ||
{ | ||
let span = init.span; | ||
cx.emit_spanned_lint(STATIC_MUT_REF, span, UseOfStaticMut { span, why_note: () }); | ||
} | ||
} | ||
} |