Skip to content

Commit

Permalink
Warn on method call mutating const, even if it has destructor
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Oct 1, 2020
1 parent eef5104 commit 75c2fdf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/lint/lint-const-item-mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
27 changes: 26 additions & 1 deletion src/test/ui/lint/lint-const-item-mutation.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 75c2fdf

Please sign in to comment.