Warn against calls which mutate an interior mutable const-item#148407
Merged
bors merged 5 commits intorust-lang:mainfrom Nov 22, 2025
Merged
Warn against calls which mutate an interior mutable const-item#148407bors merged 5 commits intorust-lang:mainfrom
const-item#148407bors merged 5 commits intorust-lang:mainfrom
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
const_item_interior_mutationsinterior_mutable_const_item_mutationssuspicious_mutation_of_interior_mutable_constswarn-by-default
The
const_item_interior_mutationslint checks for calls which mutates an interior mutable const-item.Example
Explanation
Calling a method which mutates an interior mutable type has no effect as const-item are essentially inlined wherever they are used, meaning that they are copied directly into the relevant context when used rendering modification through interior mutability ineffective across usage of that const-item.
The current implementation of this lint only warns on significant
stdandcoreinterior mutable types, likeOnce,AtomicI32, ... this is done out of prudence and may be extended in the future.This PR is an targeted alternative to #132146. It avoids false-positives by adding an internal-only attribute
#[rustc_should_not_be_called_on_const_items]on methods and functions that mutates an interior mutale type through a shared reference (mutable refrences are already linted by theconst_item_mutationlint).It should also be noted that this is NOT an uplift of the more general
clippy::borrow_interior_mutable_constlint, which is a much more general lint regarding borrow of interior mutable types, but has false-positives that are completly avoided by this lint.A simple GitHub Search reveals many instance where the user probably wanted to use a
static-item instead.@rustbot labels +I-lang-nominated +T-lang
cc @traviscross
r? compiler
Fixes IRLO - Forbidding creation of constant mutexes, etc
Fixes #132028
Fixes #40543