-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Warn against calls which mutates an interior mutable const-item
#148407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
Some changes occurred in compiler/rustc_passes/src/check_attr.rs Some changes occurred in compiler/rustc_hir/src/attrs Some changes occurred in src/tools/clippy cc @rust-lang/clippy Some changes occurred in compiler/rustc_attr_parsing |
ccc5c6c to
260a13e
Compare
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment has been minimized.
This comment has been minimized.
|
Thanks for the PR. For my part, I like the approach. Regarding the name, since this only affects const items (i.e., not e.g. const blocks), I'd probably like to have "item" or "items" in the name (as with const-item-mutation). Probably, also, I'd put the plural on "mutations" rather than on "consts" or on "items". The word "suspicious" in a lint name makes sense when only a subset of the named things are suspicious. E.g., with suspicious-double-ref-op, not all operations on double references are suspicious, but some are. For this one, do we know of any cases of mutations on interior mutable const items that aren't suspicious? (I.e., even if we're only linting a subset at the moment, due to needing to manually mark which functions actually do mutations through a shared reference.) If not, maybe I'd think to name it Probably I'd also think about whether we might want to rename const-item-mutation into |
fa928c7 to
630020b
Compare
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
|
Makes sense to me. I propose we do this. @rfcbot fcp merge |
|
Team member @traviscross has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns. |
|
For naming, |
interior_mutable_const_item_mutationssuspicious_mutation_of_interior_mutable_constswarn-by-default
The
interior_mutable_const_item_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_must_not_call_on_interior_mutable_consts]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