diff --git a/tests/ui/lint/const-item-interior-mutations-const-deref.rs b/tests/ui/lint/const-item-interior-mutations-const-deref.rs new file mode 100644 index 000000000000..ce684abacccd --- /dev/null +++ b/tests/ui/lint/const-item-interior-mutations-const-deref.rs @@ -0,0 +1,30 @@ +// Regression test for +// +// We shouldn't lint on user types, including through deref. + +//@ check-pass + +use std::cell::Cell; +use std::ops::Deref; + +// Cut down version of the issue reproducer without the thread local to just a Deref +pub struct LocalKey { + inner: T, +} + +impl Deref for LocalKey { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.inner + } +} + +const LOCAL_COUNT: LocalKey> = LocalKey { inner: Cell::new(8) }; + +fn main() { + let count = LOCAL_COUNT.get(); + //~^ WARN mutation of an interior mutable `const` + LOCAL_COUNT.set(count); + //~^ WARN mutation of an interior mutable `const` +} diff --git a/tests/ui/lint/const-item-interior-mutations-const-deref.stderr b/tests/ui/lint/const-item-interior-mutations-const-deref.stderr new file mode 100644 index 000000000000..35696d0ba100 --- /dev/null +++ b/tests/ui/lint/const-item-interior-mutations-const-deref.stderr @@ -0,0 +1,37 @@ +warning: mutation of an interior mutable `const` item with call to `get` + --> $DIR/const-item-interior-mutations-const-deref.rs:26:17 + | +LL | let count = LOCAL_COUNT.get(); + | -----------^^^^^^ + | | + | `LOCAL_COUNT` is a interior mutable `const` item of type `LocalKey>` + | + = note: each usage of a `const` item creates a new temporary + = note: only the temporaries and never the original `const LOCAL_COUNT` will be modified + = help: for more details on interior mutability see + = note: `#[warn(const_item_interior_mutations)]` on by default +help: for a shared instance of `LOCAL_COUNT`, consider making it a `static` item instead + | +LL - const LOCAL_COUNT: LocalKey> = LocalKey { inner: Cell::new(8) }; +LL + static LOCAL_COUNT: LocalKey> = LocalKey { inner: Cell::new(8) }; + | + +warning: mutation of an interior mutable `const` item with call to `set` + --> $DIR/const-item-interior-mutations-const-deref.rs:28:5 + | +LL | LOCAL_COUNT.set(count); + | -----------^^^^^^^^^^^ + | | + | `LOCAL_COUNT` is a interior mutable `const` item of type `LocalKey>` + | + = note: each usage of a `const` item creates a new temporary + = note: only the temporaries and never the original `const LOCAL_COUNT` will be modified + = help: for more details on interior mutability see +help: for a shared instance of `LOCAL_COUNT`, consider making it a `static` item instead + | +LL - const LOCAL_COUNT: LocalKey> = LocalKey { inner: Cell::new(8) }; +LL + static LOCAL_COUNT: LocalKey> = LocalKey { inner: Cell::new(8) }; + | + +warning: 2 warnings emitted +