-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
No warning on assigning to field of const that doesn't succeed #74053
Comments
I am not completely sure if this behavior is intended. But even if this is not a bug, Inline snippet struct Foo {
val: i32,
}
const FOO: Foo = Foo { val: 100 };
const fn bar() -> i32 {
FOO.val = -100;
FOO.val
}
fn main() {
println!("{}", FOO.val);
println!("{}", bar());
} |
I think this is intentional behavior base on its MIR and how const works: https://rust.godbolt.org/z/G-BPoi. |
cc rust-lang/rust-clippy#829 |
Is this better left in Clippy or better moved to Rustc? (I think the latter) |
Ahhh, I get it. Because it's always inlined, the left hand is exactly equivalent to |
Seems related to #16789. I also think we should emit an |
This doesn't require prioritisation. Removing it based on the discussion |
…, r=oli-obk Add CONST_ITEM_MUTATION lint Fixes rust-lang#74053 Fixes rust-lang#55721 This PR adds a new lint `CONST_ITEM_MUTATION`. Given an item `const FOO: SomeType = ..`, this lint fires on: * Attempting to write directly to a field (`FOO.field = some_val`) or array entry (`FOO.array_field[0] = val`) * Taking a mutable reference to the `const` item (`&mut FOO`), including through an autoderef `FOO.some_mut_self_method()` The lint message explains that since each use of a constant creates a new temporary, the original `const` item will not be modified.
…, r=oli-obk Add CONST_ITEM_MUTATION lint Fixes rust-lang#74053 Fixes rust-lang#55721 This PR adds a new lint `CONST_ITEM_MUTATION`. Given an item `const FOO: SomeType = ..`, this lint fires on: * Attempting to write directly to a field (`FOO.field = some_val`) or array entry (`FOO.array_field[0] = val`) * Taking a mutable reference to the `const` item (`&mut FOO`), including through an autoderef `FOO.some_mut_self_method()` The lint message explains that since each use of a constant creates a new temporary, the original `const` item will not be modified.
I tried this code:
I expected to see this happen:
Instead, this happened:
Meta
Occurs in Stable, Beta, and Nightly builds on the playground.
No backtrace is emitted
The text was updated successfully, but these errors were encountered: