-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
declare_interior_mutable_const triggers on array initializer #7665
Comments
Hey, thank you for the report 🙃. This is actually a difficult case, where this way of initializing the array uses the fact that each For cases like this, I would suggest adding a As this is theoretically working as intended, I'll mark this as a possible enhancement of the lint 🙃 @rustbot label -C-Bug -I-false-positive +C-enhancement |
Wouldn't that be just as surprising if the The example in the documentation also triggers borrow_interior_mutable_const, so it's not clear why this lint is needed in addition. In fact, it seems to me that any attempt to mutate the |
It shouldn't, because you can't modify constant values. Here is an example, what the lint is actually about const GLOBAL_CELL: Cell<bool> = Cell::new(false);
fn main() {
GLOBAL_CELL.set(true);
println!("{:?}", GLOBAL_CELL)
} This will print out "
You're right, those lints have some overlaps. I'm not sure why, my guess is that |
clippy doesn't like interior mutable items in `const`s, because mutating an instance of the `const` value will not mutate the const. that is the *correct* behavior here, as the const is used just as an array initializer; every time it's referenced, it *should* produce a new value. therefore, this warning is incorrect in this case. see rust-lang/rust-clippy#7665
clippy doesn't like interior mutable items in `const`s, because mutating an instance of the `const` value will not mutate the const. that is the *correct* behavior here, as the const is used just as an array initializer; every time it's referenced, it *should* produce a new value. therefore, this warning is incorrect in this case. see rust-lang/rust-clippy#7665
clippy doesn't like interior mutable items in `const`s, because mutating an instance of the `const` value will not mutate the const. that is the *correct* behavior here, as the const is used just as an array initializer; every time it's referenced, it *should* produce a new value. therefore, this warning is incorrect in this case. see rust-lang/rust-clippy#7665
clippy doesn't like interior mutable items in `const`s, because mutating an instance of the `const` value will not mutate the const. that is the *correct* behavior here, as the const is used just as an array initializer; every time it's referenced, it *should* produce a new value. therefore, this warning is incorrect in this case. see rust-lang/rust-clippy#7665
clippy doesn't like interior mutable items in `const`s, because mutating an instance of the `const` value will not mutate the const. that is the *correct* behavior here, as the const is used just as an array initializer; every time it's referenced, it *should* produce a new value. therefore, this warning is incorrect in this case. see rust-lang/rust-clippy#7665
I stumbled upon this issue. It was in code like this: static REGISTERED_NAMES: [OnceLock<String>; 256] = {
const INIT_HELPER: OnceLock<String> = OnceLock::new();
[INIT_HELPER; 256]
}; This |
As of 1.79 you can use inline const blocks (e.g. |
Lint name:
declare_interior_mutable_const
I tried this code:
Since the array initialization syntax requires
T: Copy
, rustc errors and suggests this fix that works correctly:playground
To my knowledge this is the best way to initialize an array with a
const
but!Copy
value.(stackoverflow answer)
However,
declare_interior_mutable_const
triggers and incorrectly suggests "make this a static item (maybe with lazy_static)", which doesn't actually work at all. The lint documentation claims the code is bad because "Consts are copied everywhere they are referenced, ... , which defeats the whole purpose of using these types in the first place.", but in this case that is the entire reason for using aconst
item.The known issues mention "a legacy way to supply an initialized value to downstream static items" as a legit use, but it is not immediately clear if this case is covered by that.
Meta
Rust version (
rustc -Vv
):The text was updated successfully, but these errors were encountered: