-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Allow disabling const_item_mutation lint for all uses of a specific const item #77522
Conversation
I thought about doing this - however, this has the unfortunate side effect of suppressing the lint within the initializer: const OTHER: [bool; 1] = [true];
#[allow(const_item_mutation)]
const MYCONST: u8 = {
OTHER[0] = false; // doesn't warn
0
}; While this seems unlikely to come up in practice, I would prefer if we separated lints that reference a const definition from lints that apply to a const definition (i.e. the initializer). |
Considering this in terms of false negatives, I think this approach is less likely to have problematic/surprising false negatives in practice than the one from #77251. The programmer already typed an const OTHER: [bool; 1] = [true];
const MYCONST_HELPER: u8 = {
OTHER[0] = false; //~ warning
0
};
#[allow(const_item_mutation)]
const MYCONST: u8 = MYCONST_HELPER;
I agree, though having a "shallow" way to talk about an item without talking about nested pieces like the initializer is an extremely general problem. Some examples where I have seen the same thing come up with existing lints are:
|
Will close to give a chance to design a way to distinguish lints that reference a const vs lints inside the const definition; we can follow up in #77425. |
FWIW, I agree with this idea / initiative; lints are good, and this one was direly needed, but "false positive" on lints can be quite annoying.
Aside
FWIW, I have encountered this myself, and this is how I palliate it: match $e { b => { #[allow(unused_unsafe)]] {
unsafe { ::std::intrinsics::likely(b) }
}}} or: ({
#[inline]
fn likely (b: bool) -> bool {
unsafe { ::std::intrinsics::likely(b) }
}
likely
})($e) |
Closes #77425.
The const_item_mutation lint will look at whether
allow(const_item_mutation)
is present on the const being "mutated", and if so, respect that as suppressing const_item_mutation for all uses of that const.This supersedes the special case around consts having a
Drop
impl introduced in #77251, so it is removed in this PR.r? @Aaron1011