Add lint against (some) interior mutable consts#132146
Closed
Urgau wants to merge 6 commits intorust-lang:masterfrom
Closed
Add lint against (some) interior mutable consts#132146Urgau wants to merge 6 commits intorust-lang:masterfrom
Urgau wants to merge 6 commits intorust-lang:masterfrom
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.
interior_mutable_constswarn-by-default
The
interior_mutable_constslint detects instance where const-items have a interior mutable type, which silently does nothing.Example
Explanation
Using a const-item with 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.It should be noted that the lint in not immunized against false-positives in particular when those immutable const-items are used as "init" const for const arrays (i.e.
[INIT; 10]), which is why the lint recommends inline-const instead.It should also be noted that this is NOT an uplift of the more general
clippy::declare_interior_mutable_constlint, which works for all interior mutable types, but has even more false-positives than the currently proposed 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 -S-waiting-on-review
cc @AngelicosPhosphoros @kupiakos
r? compiler
Fixes IRLO - Forbidding creation of constant mutexes, etc
Fixes #132028
Fixes #40543