rustc_lint/
interior_mutable_consts.rs1use rustc_hir::attrs::AttributeKind;
2use rustc_hir::def::{DefKind, Res};
3use rustc_hir::{Expr, ExprKind, ItemKind, Node, find_attr};
4use rustc_middle::ty::adjustment::Adjust;
5use rustc_session::{declare_lint, declare_lint_pass};
6
7use crate::lints::{ConstItemInteriorMutationsDiag, ConstItemInteriorMutationsSuggestionStatic};
8use crate::{LateContext, LateLintPass, LintContext};
9
10#[doc = r" The `const_item_interior_mutations` lint checks for calls which"]
#[doc = r" mutates an interior mutable const-item."]
#[doc = r""]
#[doc = r" ### Example"]
#[doc = r""]
#[doc = r" ```rust"]
#[doc = r" use std::sync::Once;"]
#[doc = r""]
#[doc =
r" const INIT: Once = Once::new(); // using `INIT` will always create a temporary and"]
#[doc =
r" // never modify it-self on use, should be a `static`"]
#[doc = r" // instead for shared use"]
#[doc = r""]
#[doc = r" fn init() {"]
#[doc = r" INIT.call_once(|| {"]
#[doc = r#" println!("Once::call_once first call");"#]
#[doc = r" });"]
#[doc =
r" INIT.call_once(|| { // this second will also print"]
#[doc =
r#" println!("Once::call_once second call"); // as each call to `INIT` creates"#]
#[doc = r" }); // new temporary"]
#[doc = r" }"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc =
r" Calling a method which mutates an interior mutable type has no effect as const-item"]
#[doc =
r" are essentially inlined wherever they are used, meaning that they are copied"]
#[doc =
r" directly into the relevant context when used rendering modification through"]
#[doc = r" interior mutability ineffective across usage of that const-item."]
#[doc = r""]
#[doc =
r" The current implementation of this lint only warns on significant `std` and"]
#[doc =
r" `core` interior mutable types, like `Once`, `AtomicI32`, ... this is done out"]
#[doc =
r" of prudence to avoid false-positive and may be extended in the future."]
pub static CONST_ITEM_INTERIOR_MUTATIONS: &::rustc_lint_defs::Lint =
&::rustc_lint_defs::Lint {
name: "CONST_ITEM_INTERIOR_MUTATIONS",
default_level: ::rustc_lint_defs::Warn,
desc: "checks for calls which mutates a interior mutable const-item",
is_externally_loaded: false,
..::rustc_lint_defs::Lint::default_fields_for_macro()
};declare_lint! {
11 pub CONST_ITEM_INTERIOR_MUTATIONS,
46 Warn,
47 "checks for calls which mutates a interior mutable const-item"
48}
49
50pub struct InteriorMutableConsts;
#[automatically_derived]
impl ::core::marker::Copy for InteriorMutableConsts { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for InteriorMutableConsts { }
#[automatically_derived]
impl ::core::clone::Clone for InteriorMutableConsts {
#[inline]
fn clone(&self) -> InteriorMutableConsts { *self }
}
impl ::rustc_lint_defs::LintPass for InteriorMutableConsts {
fn name(&self) -> &'static str { "InteriorMutableConsts" }
fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
<[_]>::into_vec(::alloc::boxed::box_new([CONST_ITEM_INTERIOR_MUTATIONS]))
}
}
impl InteriorMutableConsts {
#[allow(unused)]
pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
<[_]>::into_vec(::alloc::boxed::box_new([CONST_ITEM_INTERIOR_MUTATIONS]))
}
}declare_lint_pass!(InteriorMutableConsts => [CONST_ITEM_INTERIOR_MUTATIONS]);
51
52impl<'tcx> LateLintPass<'tcx> for InteriorMutableConsts {
53 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
54 let typeck = cx.typeck_results();
55
56 let (method_did, receiver) = match expr.kind {
57 ExprKind::MethodCall(_, receiver, _, _) => {
59 (typeck.type_dependent_def_id(expr.hir_id), receiver)
60 }
61 ExprKind::Call(path, [receiver, ..]) => match receiver.kind {
63 ExprKind::AddrOf(_, _, receiver) => match path.kind {
64 ExprKind::Path(ref qpath) => {
65 (cx.qpath_res(qpath, path.hir_id).opt_def_id(), receiver)
66 }
67 _ => return,
68 },
69 _ => return,
70 },
71 _ => return,
72 };
73
74 let Some(method_did) = method_did else {
75 return;
76 };
77
78 if let ExprKind::Path(qpath) = &receiver.kind
79 && let Res::Def(DefKind::Const | DefKind::AssocConst, const_did) =
80 typeck.qpath_res(qpath, receiver.hir_id)
81 && !cx
84 .typeck_results()
85 .expr_adjustments(receiver)
86 .into_iter()
87 .any(|adj| #[allow(non_exhaustive_omitted_patterns)] match adj.kind {
Adjust::Deref(_) => true,
_ => false,
}matches!(adj.kind, Adjust::Deref(_)))
88 && {
{
'done:
{
for i in cx.tcx.get_all_attrs(method_did) {
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(AttributeKind::RustcShouldNotBeCalledOnConstItems(_))
=> {
break 'done Some(());
}
_ => {}
}
}
None
}
}.is_some()
}find_attr!(
90 cx.tcx.get_all_attrs(method_did),
91 AttributeKind::RustcShouldNotBeCalledOnConstItems(_)
92 )
93 && let Some(method_name) = cx.tcx.opt_item_ident(method_did)
94 && let Some(const_name) = cx.tcx.opt_item_ident(const_did)
95 && let Some(const_ty) = typeck.node_type_opt(receiver.hir_id)
96 {
97 let sugg_static = if let Some(Node::Item(const_item)) =
99 cx.tcx.hir_get_if_local(const_did)
100 && let ItemKind::Const(ident, _generics, _ty, _body_id) = const_item.kind
101 {
102 if let Some(vis_span) = const_item.vis_span.find_ancestor_inside(const_item.span)
103 && const_item.span.can_be_used_for_suggestions()
104 && vis_span.can_be_used_for_suggestions()
105 {
106 Some(ConstItemInteriorMutationsSuggestionStatic::Spanful {
107 const_: const_item.vis_span.between(ident.span),
108 before: if !vis_span.is_empty() { " " } else { "" },
109 })
110 } else {
111 Some(ConstItemInteriorMutationsSuggestionStatic::Spanless)
112 }
113 } else {
114 None
115 };
116
117 cx.emit_span_lint(
118 CONST_ITEM_INTERIOR_MUTATIONS,
119 expr.span,
120 ConstItemInteriorMutationsDiag {
121 method_name,
122 const_name,
123 const_ty,
124 receiver_span: receiver.span,
125 sugg_static,
126 },
127 );
128 }
129 }
130}