Warn on method call mutating const, even if it has destructor

This commit is contained in:
David Tolnay 2020-09-30 22:16:32 -07:00
parent eef5104c53
commit 75c2fdf34a
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
3 changed files with 28 additions and 3 deletions

View File

@ -111,7 +111,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ConstMutationChecker<'a, 'tcx> {
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, loc: Location) {
if let Rvalue::Ref(_, BorrowKind::Mut { .. }, place) = rvalue {
let local = place.local;
if let Some(def_id) = self.is_const_item_without_destructor(local) {
if let Some(def_id) = self.is_const_item(local) {
// If this Rvalue is being used as the right-hand side of a
// `StatementKind::Assign`, see if it ends up getting used as
// the `self` parameter of a method call (as the terminator of our current

View File

@ -49,5 +49,5 @@ fn main() {
MUTABLE.msg = "wow"; // no warning, because Drop observes the mutation
MUTABLE2.msg = "wow"; //~ WARN attempting to modify
VEC.push(0); // no warning
VEC.push(0); //~ WARN taking a mutable reference to a `const` item
}

View File

@ -98,5 +98,30 @@ note: `const` item defined here
LL | const MUTABLE2: Mutable2 = Mutable2 { msg: "", other: String::new() };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: 7 warnings emitted
warning: taking a mutable reference to a `const` item
--> $DIR/lint-const-item-mutation.rs:52:5
|
LL | VEC.push(0);
| ^^^^^^^^^^^
|
= note: each usage of a `const` item creates a new temporary
= note: the mutable reference will refer to this temporary, not the original `const` item
note: mutable reference created due to call to this method
--> $SRC_DIR/alloc/src/vec.rs:LL:COL
|
LL | / pub fn push(&mut self, value: T) {
LL | | // This will panic or abort if we would allocate > isize::MAX bytes
LL | | // or if the length increment would overflow for zero-sized types.
LL | | if self.len == self.buf.capacity() {
... |
LL | | }
LL | | }
| |_____^
note: `const` item defined here
--> $DIR/lint-const-item-mutation.rs:31:1
|
LL | const VEC: Vec<i32> = Vec::new();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: 8 warnings emitted