Skip to main content

rustc_lint/
interior_mutable_consts.rs

1use rustc_hir::def::{DefKind, Res};
2use rustc_hir::{Expr, ExprKind, ItemKind, Node, find_attr};
3use rustc_middle::ty::adjustment::Adjust;
4use rustc_session::{declare_lint, declare_lint_pass};
5
6use crate::lints::{ConstItemInteriorMutationsDiag, ConstItemInteriorMutationsSuggestionStatic};
7use crate::{LateContext, LateLintPass, LintContext};
8
9#[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! {
10    /// The `const_item_interior_mutations` lint checks for calls which
11    /// mutates an interior mutable const-item.
12    ///
13    /// ### Example
14    ///
15    /// ```rust
16    /// use std::sync::Once;
17    ///
18    /// const INIT: Once = Once::new(); // using `INIT` will always create a temporary and
19    ///                                 // never modify it-self on use, should be a `static`
20    ///                                 // instead for shared use
21    ///
22    /// fn init() {
23    ///     INIT.call_once(|| {
24    ///         println!("Once::call_once first call");
25    ///     });
26    ///     INIT.call_once(|| {                          // this second will also print
27    ///         println!("Once::call_once second call"); // as each call to `INIT` creates
28    ///     });                                          // new temporary
29    /// }
30    /// ```
31    ///
32    /// {{produces}}
33    ///
34    /// ### Explanation
35    ///
36    /// Calling a method which mutates an interior mutable type has no effect as const-item
37    /// are essentially inlined wherever they are used, meaning that they are copied
38    /// directly into the relevant context when used rendering modification through
39    /// interior mutability ineffective across usage of that const-item.
40    ///
41    /// The current implementation of this lint only warns on significant `std` and
42    /// `core` interior mutable types, like `Once`, `AtomicI32`, ... this is done out
43    /// of prudence to avoid false-positive and may be extended in the future.
44    pub CONST_ITEM_INTERIOR_MUTATIONS,
45    Warn,
46    "checks for calls which mutates a interior mutable const-item"
47}
48
49pub 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 {
        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                [CONST_ITEM_INTERIOR_MUTATIONS]))
    }
}
impl InteriorMutableConsts {
    #[allow(unused)]
    pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                [CONST_ITEM_INTERIOR_MUTATIONS]))
    }
}declare_lint_pass!(InteriorMutableConsts => [CONST_ITEM_INTERIOR_MUTATIONS]);
50
51impl<'tcx> LateLintPass<'tcx> for InteriorMutableConsts {
52    fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
53        let typeck = cx.typeck_results();
54
55        let (method_did, receiver) = match expr.kind {
56            // matching on `<receiver>.method(..)`
57            ExprKind::MethodCall(_, receiver, _, _) => {
58                (typeck.type_dependent_def_id(expr.hir_id), receiver)
59            }
60            // matching on `function(&<receiver>, ...)`
61            ExprKind::Call(path, [receiver, ..]) => match receiver.kind {
62                ExprKind::AddrOf(_, _, receiver) => match path.kind {
63                    ExprKind::Path(ref qpath) => {
64                        (cx.qpath_res(qpath, path.hir_id).opt_def_id(), receiver)
65                    }
66                    _ => return,
67                },
68                _ => return,
69            },
70            _ => return,
71        };
72
73        let Some(method_did) = method_did else {
74            return;
75        };
76
77        if let ExprKind::Path(qpath) = &receiver.kind
78            && let Res::Def(DefKind::Const | DefKind::AssocConst, const_did) =
79                typeck.qpath_res(qpath, receiver.hir_id)
80            // Don't consider derefs as those can do arbitrary things
81            // like using thread local (see rust-lang/rust#150157)
82            && !cx
83                .typeck_results()
84                .expr_adjustments(receiver)
85                .into_iter()
86                .any(|adj| #[allow(non_exhaustive_omitted_patterns)] match adj.kind {
    Adjust::Deref(_) => true,
    _ => false,
}matches!(adj.kind, Adjust::Deref(_)))
87            // Let's do the attribute check after the other checks for perf reasons
88            && {

        #[allow(deprecated)]
        {
            {
                'done:
                    {
                    for i in cx.tcx.get_all_attrs(method_did) {
                        #[allow(unused_imports)]
                        use rustc_hir::attrs::AttributeKind::*;
                        let i: &rustc_hir::Attribute = i;
                        match i {
                            rustc_hir::Attribute::Parsed(RustcShouldNotBeCalledOnConstItems(_))
                                => {
                                break 'done Some(());
                            }
                            rustc_hir::Attribute::Unparsed(..) =>
                                {}
                                #[deny(unreachable_patterns)]
                                _ => {}
                        }
                    }
                    None
                }
            }
        }
    }.is_some()find_attr!(
89                cx.tcx, method_did,
90                RustcShouldNotBeCalledOnConstItems(_)
91            )
92            && let Some(method_name) = cx.tcx.opt_item_ident(method_did)
93            && let Some(const_name) = cx.tcx.opt_item_ident(const_did)
94            && let Some(const_ty) = typeck.node_type_opt(receiver.hir_id)
95        {
96            // Find the local `const`-item and create the suggestion to use `static` instead
97            let sugg_static = if let Some(Node::Item(const_item)) =
98                cx.tcx.hir_get_if_local(const_did)
99                && let ItemKind::Const(ident, _generics, _ty, _body_id) = const_item.kind
100            {
101                if let Some(vis_span) = const_item.vis_span.find_ancestor_inside(const_item.span)
102                    && const_item.span.can_be_used_for_suggestions()
103                    && vis_span.can_be_used_for_suggestions()
104                {
105                    Some(ConstItemInteriorMutationsSuggestionStatic::Spanful {
106                        const_: const_item.vis_span.between(ident.span),
107                        before: if !vis_span.is_empty() { " " } else { "" },
108                    })
109                } else {
110                    Some(ConstItemInteriorMutationsSuggestionStatic::Spanless)
111                }
112            } else {
113                None
114            };
115
116            cx.emit_span_lint(
117                CONST_ITEM_INTERIOR_MUTATIONS,
118                expr.span,
119                ConstItemInteriorMutationsDiag {
120                    method_name,
121                    const_name,
122                    const_ty,
123                    receiver_span: receiver.span,
124                    sugg_static,
125                },
126            );
127        }
128    }
129}