Skip to content
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

Way to disable const_item_mutation lint for one specific const #77425

Open
dtolnay opened this issue Oct 1, 2020 · 0 comments
Open

Way to disable const_item_mutation lint for one specific const #77425

dtolnay opened this issue Oct 1, 2020 · 0 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dtolnay
Copy link
Member

dtolnay commented Oct 1, 2020

The lint added in Rust 1.48 by #75573 warns on anything that looks like mutation of a const, such as CONST.field = some_val or VEC.push(0).

However, there is at least one compelling reason that a library would want one of its const items with a Drop impl to not trigger that new lint in downstream code. See #77251 (comment). We use CFG.field = some_val to expose safe global configuration of a library in a way that deliberately strongly triggers "okay I am writing to a global (though safely)" neurons, which an alternative like ::set_whatever(...) does not.

#77251 weakens the lint to avoid triggering on expressions resembling CONST.field = some_val when there is a Drop impl on CONST, though still triggering on VEC.push(0) i.e. when a const with a Drop impl is used as a &mut self receiver.

However, this compromise is likely to hit false negatives that reduce the overall usefulness of the const item mutation lint. We would rather have a (likely attribute-based) way to disable the lint at the granularity of specific const items only. Something substantially equivalent to:

#[const_mutation_allowed]
pub const CFG: $t = $v;

An actual such attribute still requires more design work, since there are similar lints that we are interested in creating/uplifting in the future (e.g. const items with interior mutability) which also will need granular opt outs, and it may not be ideal to introduce a single-purpose new opt-out attribute for each one of them.

@Aaron1011

@jyn514 jyn514 added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants