Don't lint on interior mutable const item coming from derefs

(cherry picked from commit 2581c2571c)
This commit is contained in:
Urgau 2025-12-19 18:13:07 +01:00 committed by Josh Stone
parent 0aa11b5dd2
commit 249d399caa
3 changed files with 8 additions and 39 deletions

View file

@ -1,6 +1,7 @@
use rustc_hir::attrs::AttributeKind;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::{Expr, ExprKind, ItemKind, Node, find_attr};
use rustc_middle::ty::adjustment::Adjust;
use rustc_session::{declare_lint, declare_lint_pass};
use crate::lints::{ConstItemInteriorMutationsDiag, ConstItemInteriorMutationsSuggestionStatic};
@ -77,6 +78,13 @@ impl<'tcx> LateLintPass<'tcx> for InteriorMutableConsts {
if let ExprKind::Path(qpath) = &receiver.kind
&& let Res::Def(DefKind::Const | DefKind::AssocConst, const_did) =
typeck.qpath_res(qpath, receiver.hir_id)
// Don't consider derefs as those can do arbitrary things
// like using thread local (see rust-lang/rust#150157)
&& !cx
.typeck_results()
.expr_adjustments(receiver)
.into_iter()
.any(|adj| matches!(adj.kind, Adjust::Deref(_)))
// Let's do the attribute check after the other checks for perf reasons
&& find_attr!(
cx.tcx.get_all_attrs(method_did),

View file

@ -24,7 +24,5 @@ const LOCAL_COUNT: LocalKey<Cell<usize>> = LocalKey { inner: Cell::new(8) };
fn main() {
let count = LOCAL_COUNT.get();
//~^ WARN mutation of an interior mutable `const`
LOCAL_COUNT.set(count);
//~^ WARN mutation of an interior mutable `const`
}

View file

@ -1,37 +0,0 @@
warning: mutation of an interior mutable `const` item with call to `get`
--> $DIR/const-item-interior-mutations-const-deref.rs:26:17
|
LL | let count = LOCAL_COUNT.get();
| -----------^^^^^^
| |
| `LOCAL_COUNT` is a interior mutable `const` item of type `LocalKey<Cell<usize>>`
|
= note: each usage of a `const` item creates a new temporary
= note: only the temporaries and never the original `const LOCAL_COUNT` will be modified
= help: for more details on interior mutability see <https://doc.rust-lang.org/reference/interior-mutability.html>
= note: `#[warn(const_item_interior_mutations)]` on by default
help: for a shared instance of `LOCAL_COUNT`, consider making it a `static` item instead
|
LL - const LOCAL_COUNT: LocalKey<Cell<usize>> = LocalKey { inner: Cell::new(8) };
LL + static LOCAL_COUNT: LocalKey<Cell<usize>> = LocalKey { inner: Cell::new(8) };
|
warning: mutation of an interior mutable `const` item with call to `set`
--> $DIR/const-item-interior-mutations-const-deref.rs:28:5
|
LL | LOCAL_COUNT.set(count);
| -----------^^^^^^^^^^^
| |
| `LOCAL_COUNT` is a interior mutable `const` item of type `LocalKey<Cell<usize>>`
|
= note: each usage of a `const` item creates a new temporary
= note: only the temporaries and never the original `const LOCAL_COUNT` will be modified
= help: for more details on interior mutability see <https://doc.rust-lang.org/reference/interior-mutability.html>
help: for a shared instance of `LOCAL_COUNT`, consider making it a `static` item instead
|
LL - const LOCAL_COUNT: LocalKey<Cell<usize>> = LocalKey { inner: Cell::new(8) };
LL + static LOCAL_COUNT: LocalKey<Cell<usize>> = LocalKey { inner: Cell::new(8) };
|
warning: 2 warnings emitted