Warn on method call mutating const, even if it has destructor
This commit is contained in:
parent
eef5104c53
commit
75c2fdf34a
3 changed files with 28 additions and 3 deletions
|
@ -111,7 +111,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ConstMutationChecker<'a, 'tcx> {
|
||||||
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, loc: Location) {
|
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, loc: Location) {
|
||||||
if let Rvalue::Ref(_, BorrowKind::Mut { .. }, place) = rvalue {
|
if let Rvalue::Ref(_, BorrowKind::Mut { .. }, place) = rvalue {
|
||||||
let local = place.local;
|
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
|
// If this Rvalue is being used as the right-hand side of a
|
||||||
// `StatementKind::Assign`, see if it ends up getting used as
|
// `StatementKind::Assign`, see if it ends up getting used as
|
||||||
// the `self` parameter of a method call (as the terminator of our current
|
// the `self` parameter of a method call (as the terminator of our current
|
||||||
|
|
|
@ -49,5 +49,5 @@ fn main() {
|
||||||
|
|
||||||
MUTABLE.msg = "wow"; // no warning, because Drop observes the mutation
|
MUTABLE.msg = "wow"; // no warning, because Drop observes the mutation
|
||||||
MUTABLE2.msg = "wow"; //~ WARN attempting to modify
|
MUTABLE2.msg = "wow"; //~ WARN attempting to modify
|
||||||
VEC.push(0); // no warning
|
VEC.push(0); //~ WARN taking a mutable reference to a `const` item
|
||||||
}
|
}
|
||||||
|
|
|
@ -98,5 +98,30 @@ note: `const` item defined here
|
||||||
LL | const MUTABLE2: Mutable2 = Mutable2 { msg: "", other: String::new() };
|
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
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue