1
Fork 0

Auto merge of #75573 - Aaron1011:feature/const-mutation-lint, r=oli-obk

Add CONST_ITEM_MUTATION lint

Fixes #74053
Fixes #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.
This commit is contained in:
bors 2020-09-10 05:54:26 +00:00
commit 88197214b8
22 changed files with 427 additions and 116 deletions

View file

@ -232,6 +232,12 @@ declare_lint! {
"detects unaligned references to fields of packed structs",
}
declare_lint! {
pub CONST_ITEM_MUTATION,
Warn,
"detects attempts to mutate a `const` item",
}
declare_lint! {
pub SAFE_PACKED_BORROWS,
Warn,
@ -582,6 +588,7 @@ declare_lint_pass! {
CONST_ERR,
RENAMED_AND_REMOVED_LINTS,
UNALIGNED_REFERENCES,
CONST_ITEM_MUTATION,
SAFE_PACKED_BORROWS,
PATTERNS_IN_FNS_WITHOUT_BODY,
LATE_BOUND_LIFETIME_ARGUMENTS,