summary refs log tree commit diff
path: root/compiler/rustc_session/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-09-10 05:54:26 +0000
committerbors <bors@rust-lang.org>2020-09-10 05:54:26 +0000
commit88197214b8a9099bb3da559a3bd7bf4867c10c5f (patch)
treeac629e272c836b5dd523baec4209f32110843244 /compiler/rustc_session/src
parenta1894e4afe1a39f718cc27232a5a2f0d02b501f6 (diff)
parent4434e8cefbec96e6928ea23769eb1a83d0f198b5 (diff)
downloadrust-88197214b8a9099bb3da559a3bd7bf4867c10c5f.tar.gz
rust-88197214b8a9099bb3da559a3bd7bf4867c10c5f.zip
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.
Diffstat (limited to 'compiler/rustc_session/src')
-rw-r--r--compiler/rustc_session/src/lint/builtin.rs7
1 files changed, 7 insertions, 0 deletions
diff --git a/compiler/rustc_session/src/lint/builtin.rs b/compiler/rustc_session/src/lint/builtin.rs
index 2bcf10b8b38..a9deaaae0da 100644
--- a/compiler/rustc_session/src/lint/builtin.rs
+++ b/compiler/rustc_session/src/lint/builtin.rs
@@ -233,6 +233,12 @@ declare_lint! {
 }
 
 declare_lint! {
+    pub CONST_ITEM_MUTATION,
+    Warn,
+    "detects attempts to mutate a `const` item",
+}
+
+declare_lint! {
     pub SAFE_PACKED_BORROWS,
     Warn,
     "safe borrows of fields of packed structs were erroneously allowed",
@@ -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,