Don't fire CONST_ITEM_MUTATION lint when borrowing a deref

Fixes #78819

This extends the check for dereferences added in PR #77324
to cover mutable borrows, as well as direct writes. If we're operating
on a dereference of a `const` item, we shouldn't be firing the lint.
This commit is contained in:
Aaron Hill 2020-11-07 20:11:53 -05:00
parent b2d115f6db
commit e4e9bb4a24
No known key found for this signature in database
GPG key ID: B4087E510E98B164
3 changed files with 43 additions and 29 deletions

View file

@ -29,6 +29,7 @@ const RAW_PTR: *mut u8 = 1 as *mut u8;
const MUTABLE: Mutable = Mutable { msg: "" };
const MUTABLE2: Mutable2 = Mutable2 { msg: "", other: String::new() };
const VEC: Vec<i32> = Vec::new();
const PTR: *mut () = 1 as *mut _;
fn main() {
ARRAY[0] = 5; //~ WARN attempting to modify
@ -50,4 +51,8 @@ fn main() {
MUTABLE.msg = "wow"; // no warning, because Drop observes the mutation
MUTABLE2.msg = "wow"; //~ WARN attempting to modify
VEC.push(0); //~ WARN taking a mutable reference to a `const` item
// Test that we don't warn when converting a raw pointer
// into a mutable reference
unsafe { &mut *PTR };
}

View file

@ -1,5 +1,5 @@
warning: attempting to modify a `const` item
--> $DIR/lint-const-item-mutation.rs:34:5
--> $DIR/lint-const-item-mutation.rs:35:5
|
LL | ARRAY[0] = 5;
| ^^^^^^^^^^^^
@ -13,7 +13,7 @@ LL | const ARRAY: [u8; 1] = [25];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: attempting to modify a `const` item
--> $DIR/lint-const-item-mutation.rs:35:5
--> $DIR/lint-const-item-mutation.rs:36:5
|
LL | MY_STRUCT.field = false;
| ^^^^^^^^^^^^^^^^^^^^^^^
@ -26,7 +26,7 @@ LL | const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: attempting to modify a `const` item
--> $DIR/lint-const-item-mutation.rs:36:5
--> $DIR/lint-const-item-mutation.rs:37:5
|
LL | MY_STRUCT.inner_array[0] = 'b';
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -39,7 +39,7 @@ LL | const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: taking a mutable reference to a `const` item
--> $DIR/lint-const-item-mutation.rs:37:5
--> $DIR/lint-const-item-mutation.rs:38:5
|
LL | MY_STRUCT.use_mut();
| ^^^^^^^^^^^^^^^^^^^
@ -58,7 +58,7 @@ LL | const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: taking a mutable reference to a `const` item
--> $DIR/lint-const-item-mutation.rs:38:5
--> $DIR/lint-const-item-mutation.rs:39:5
|
LL | &mut MY_STRUCT;
| ^^^^^^^^^^^^^^
@ -72,7 +72,7 @@ LL | const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: taking a mutable reference to a `const` item
--> $DIR/lint-const-item-mutation.rs:39:5
--> $DIR/lint-const-item-mutation.rs:40:5
|
LL | (&mut MY_STRUCT).use_mut();
| ^^^^^^^^^^^^^^^^
@ -86,7 +86,7 @@ LL | const MY_STRUCT: MyStruct = MyStruct { field: true, inner_array: ['a'], raw
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: attempting to modify a `const` item
--> $DIR/lint-const-item-mutation.rs:51:5
--> $DIR/lint-const-item-mutation.rs:52:5
|
LL | MUTABLE2.msg = "wow";
| ^^^^^^^^^^^^^^^^^^^^
@ -99,7 +99,7 @@ LL | const MUTABLE2: Mutable2 = Mutable2 { msg: "", other: String::new() };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: taking a mutable reference to a `const` item
--> $DIR/lint-const-item-mutation.rs:52:5
--> $DIR/lint-const-item-mutation.rs:53:5
|
LL | VEC.push(0);
| ^^^^^^^^^^^