Skip to content

Way to disable const_item_mutation lint for one specific const #77425

Open
@dtolnay

Description

@dtolnay

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions