Rewrite non_copy_const

This commit is contained in:
Jason Newcomb 2024-08-03 00:29:23 -04:00
parent 95d7eda0b4
commit 57782e0ff1
21 changed files with 1010 additions and 706 deletions

View file

@ -19,7 +19,7 @@ const UNFROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(true));
const FROZEN_VARIANT: OptionalCell = OptionalCell::Frozen;
fn borrow_optional_cell() {
let _ = &UNFROZEN_VARIANT; //~ ERROR: interior mutability
let _ = &UNFROZEN_VARIANT; //~ borrow_interior_mutable_const
let _ = &FROZEN_VARIANT;
}
@ -34,11 +34,11 @@ trait AssocConsts {
// This is the "suboptimal behavior" mentioned in `is_value_unfrozen`
// caused by a similar reason to unfrozen types without any default values
// get linted even if it has frozen variants'.
let _ = &Self::TO_BE_FROZEN_VARIANT; //~ ERROR: interior mutability
let _ = &Self::TO_BE_FROZEN_VARIANT;
// The lint ignores default values because an impl of this trait can set
// an unfrozen variant to `DEFAULTED_ON_FROZEN_VARIANT` and use the default impl for `function`.
let _ = &Self::DEFAULTED_ON_FROZEN_VARIANT; //~ ERROR: interior mutability
let _ = &Self::DEFAULTED_ON_FROZEN_VARIANT;
}
}
@ -47,9 +47,9 @@ impl AssocConsts for u64 {
const TO_BE_FROZEN_VARIANT: OptionalCell = OptionalCell::Frozen;
fn function() {
let _ = &<Self as AssocConsts>::TO_BE_UNFROZEN_VARIANT; //~ ERROR: interior mutability
let _ = &<Self as AssocConsts>::TO_BE_UNFROZEN_VARIANT; //~ borrow_interior_mutable_const
let _ = &<Self as AssocConsts>::TO_BE_FROZEN_VARIANT;
let _ = &Self::DEFAULTED_ON_UNFROZEN_VARIANT; //~ ERROR: interior mutability
let _ = &Self::DEFAULTED_ON_UNFROZEN_VARIANT; //~ borrow_interior_mutable_const
let _ = &Self::DEFAULTED_ON_FROZEN_VARIANT;
}
}
@ -71,7 +71,7 @@ impl AssocTypes for u64 {
const TO_BE_FROZEN_VARIANT: Option<Self::ToBeUnfrozen> = None;
fn function() {
let _ = &<Self as AssocTypes>::TO_BE_UNFROZEN_VARIANT; //~ ERROR: interior mutability
let _ = &<Self as AssocTypes>::TO_BE_UNFROZEN_VARIANT; //~ borrow_interior_mutable_const
let _ = &<Self as AssocTypes>::TO_BE_FROZEN_VARIANT;
}
}
@ -88,14 +88,14 @@ impl<T> BothOfCellAndGeneric<T> {
const FROZEN_VARIANT: BothOfCellAndGeneric<T> = BothOfCellAndGeneric::Frozen(5);
fn function() {
let _ = &Self::UNFROZEN_VARIANT; //~ ERROR: interior mutability
let _ = &Self::GENERIC_VARIANT; //~ ERROR: interior mutability
let _ = &Self::UNFROZEN_VARIANT; //~ borrow_interior_mutable_const
let _ = &Self::GENERIC_VARIANT;
let _ = &Self::FROZEN_VARIANT;
}
}
fn main() {
// constants defined in foreign crates
let _ = &helper::WRAPPED_PRIVATE_UNFROZEN_VARIANT; //~ ERROR: interior mutability
let _ = &helper::WRAPPED_PRIVATE_UNFROZEN_VARIANT; //~ borrow_interior_mutable_const
let _ = &helper::WRAPPED_PRIVATE_FROZEN_VARIANT;
}

View file

@ -1,8 +1,8 @@
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/enums.rs:22:14
--> tests/ui/borrow_interior_mutable_const/enums.rs:22:13
|
LL | let _ = &UNFROZEN_VARIANT;
| ^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
note: the lint level is defined here
@ -12,68 +12,44 @@ LL | #![deny(clippy::borrow_interior_mutable_const)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/enums.rs:37:18
|
LL | let _ = &Self::TO_BE_FROZEN_VARIANT;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/enums.rs:41:18
|
LL | let _ = &Self::DEFAULTED_ON_FROZEN_VARIANT;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/enums.rs:50:18
--> tests/ui/borrow_interior_mutable_const/enums.rs:50:17
|
LL | let _ = &<Self as AssocConsts>::TO_BE_UNFROZEN_VARIANT;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/enums.rs:52:18
--> tests/ui/borrow_interior_mutable_const/enums.rs:52:17
|
LL | let _ = &Self::DEFAULTED_ON_UNFROZEN_VARIANT;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/enums.rs:74:18
--> tests/ui/borrow_interior_mutable_const/enums.rs:74:17
|
LL | let _ = &<Self as AssocTypes>::TO_BE_UNFROZEN_VARIANT;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/enums.rs:91:18
--> tests/ui/borrow_interior_mutable_const/enums.rs:91:17
|
LL | let _ = &Self::UNFROZEN_VARIANT;
| ^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/enums.rs:92:18
|
LL | let _ = &Self::GENERIC_VARIANT;
| ^^^^^^^^^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/enums.rs:99:14
--> tests/ui/borrow_interior_mutable_const/enums.rs:99:13
|
LL | let _ = &helper::WRAPPED_PRIVATE_UNFROZEN_VARIANT;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: aborting due to 9 previous errors
error: aborting due to 6 previous errors

View file

@ -62,20 +62,14 @@ mod issue12979 {
const CELL_REF: StaticRef<(UnsafeCell<u32>,)> = unsafe { StaticRef::new(std::ptr::null()) };
fn main() {
ATOMIC.store(1, Ordering::SeqCst);
//~^ borrow_interior_mutable_const
assert_eq!(ATOMIC.load(Ordering::SeqCst), 5);
//~^ borrow_interior_mutable_const
ATOMIC.store(1, Ordering::SeqCst); //~ borrow_interior_mutable_const
assert_eq!(ATOMIC.load(Ordering::SeqCst), 5); //~ borrow_interior_mutable_const
let _once = ONCE_INIT;
let _once_ref = &ONCE_INIT;
//~^ borrow_interior_mutable_const
let _once_ref_2 = &&ONCE_INIT;
//~^ borrow_interior_mutable_const
let _once_ref_4 = &&&&ONCE_INIT;
//~^ borrow_interior_mutable_const
let _once_mut = &mut ONCE_INIT;
//~^ borrow_interior_mutable_const
let _once_ref = &ONCE_INIT; //~ borrow_interior_mutable_const
let _once_ref_2 = &&ONCE_INIT; //~ borrow_interior_mutable_const
let _once_ref_4 = &&&&ONCE_INIT; //~ borrow_interior_mutable_const
let _once_mut = &mut ONCE_INIT; //~ borrow_interior_mutable_const
let _atomic_into_inner = ATOMIC.into_inner();
// these should be all fine.
let _twice = (ONCE_INIT, ONCE_INIT);
@ -86,30 +80,22 @@ fn main() {
let _ref_array_once = &[ONCE_INIT, ONCE_INIT][0];
// referencing projection is still bad.
let _ = &ATOMIC_TUPLE;
//~^ borrow_interior_mutable_const
let _ = &ATOMIC_TUPLE.0;
//~^ borrow_interior_mutable_const
let _ = &(&&&&ATOMIC_TUPLE).0;
//~^ borrow_interior_mutable_const
let _ = &ATOMIC_TUPLE.0[0];
//~^ borrow_interior_mutable_const
let _ = ATOMIC_TUPLE.0[0].load(Ordering::SeqCst);
//~^ borrow_interior_mutable_const
let _ = &ATOMIC_TUPLE; //~ borrow_interior_mutable_const
let _ = &ATOMIC_TUPLE.0; //~ borrow_interior_mutable_const
let _ = &(&&&&ATOMIC_TUPLE).0; //~ borrow_interior_mutable_const
let _ = &ATOMIC_TUPLE.0[0]; //~ borrow_interior_mutable_const
let _ = ATOMIC_TUPLE.0[0].load(Ordering::SeqCst); //~ borrow_interior_mutable_const
let _ = &ATOMIC_TUPLE.2;
let _ = (&&&&ATOMIC_TUPLE).0;
let _ = (&&&&ATOMIC_TUPLE).2;
let _ = (&&&&ATOMIC_TUPLE).0; //~ borrow_interior_mutable_const
let _ = (&&&&ATOMIC_TUPLE).2; //~ borrow_interior_mutable_const
let _ = ATOMIC_TUPLE.0;
let _ = ATOMIC_TUPLE.0[0];
//~^ borrow_interior_mutable_const
let _ = ATOMIC_TUPLE.1.into_iter();
let _ = ATOMIC_TUPLE.2;
let _ = &{ ATOMIC_TUPLE };
CELL.set(2);
//~^ borrow_interior_mutable_const
assert_eq!(CELL.get(), 6);
//~^ borrow_interior_mutable_const
CELL.set(2); //~ borrow_interior_mutable_const
assert_eq!(CELL.get(), 6); //~ borrow_interior_mutable_const
assert_eq!(INTEGER, 8);
assert!(STRING.is_empty());

View file

@ -4,6 +4,7 @@ error: a `const` item with interior mutability should not be borrowed
LL | ATOMIC.store(1, Ordering::SeqCst);
| ^^^^^^
|
= note: there is a compiler inserted borrow here
= help: assign this const to a local or static variable, and use the variable here
note: the lint level is defined here
--> tests/ui/borrow_interior_mutable_const/others.rs:1:9
@ -12,108 +13,120 @@ LL | #![deny(clippy::borrow_interior_mutable_const)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/others.rs:67:16
--> tests/ui/borrow_interior_mutable_const/others.rs:66:16
|
LL | assert_eq!(ATOMIC.load(Ordering::SeqCst), 5);
| ^^^^^^
|
= note: there is a compiler inserted borrow here
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/others.rs:71:22
--> tests/ui/borrow_interior_mutable_const/others.rs:69:21
|
LL | let _once_ref = &ONCE_INIT;
| ^^^^^^^^^
| ^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/others.rs:73:25
--> tests/ui/borrow_interior_mutable_const/others.rs:70:24
|
LL | let _once_ref_2 = &&ONCE_INIT;
| ^^^^^^^^^
| ^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/others.rs:75:27
--> tests/ui/borrow_interior_mutable_const/others.rs:71:26
|
LL | let _once_ref_4 = &&&&ONCE_INIT;
| ^^^^^^^^^
| ^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/others.rs:77:26
--> tests/ui/borrow_interior_mutable_const/others.rs:72:21
|
LL | let _once_mut = &mut ONCE_INIT;
| ^^^^^^^^^
| ^^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/others.rs:89:14
--> tests/ui/borrow_interior_mutable_const/others.rs:83:13
|
LL | let _ = &ATOMIC_TUPLE;
| ^^^^^^^^^^^^
| ^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/others.rs:91:14
--> tests/ui/borrow_interior_mutable_const/others.rs:84:13
|
LL | let _ = &ATOMIC_TUPLE.0;
| ^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/others.rs:93:19
--> tests/ui/borrow_interior_mutable_const/others.rs:85:18
|
LL | let _ = &(&&&&ATOMIC_TUPLE).0;
| ^^^^^^^^^^^^
| ^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/others.rs:95:14
--> tests/ui/borrow_interior_mutable_const/others.rs:86:13
|
LL | let _ = &ATOMIC_TUPLE.0[0];
| ^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/others.rs:97:13
--> tests/ui/borrow_interior_mutable_const/others.rs:87:13
|
LL | let _ = ATOMIC_TUPLE.0[0].load(Ordering::SeqCst);
| ^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^
|
= note: there is a compiler inserted borrow here
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/others.rs:89:17
|
LL | let _ = (&&&&ATOMIC_TUPLE).0;
| ^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/others.rs:103:13
--> tests/ui/borrow_interior_mutable_const/others.rs:90:17
|
LL | let _ = ATOMIC_TUPLE.0[0];
| ^^^^^^^^^^^^
LL | let _ = (&&&&ATOMIC_TUPLE).2;
| ^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/others.rs:109:5
--> tests/ui/borrow_interior_mutable_const/others.rs:97:5
|
LL | CELL.set(2);
| ^^^^
|
= note: there is a compiler inserted borrow here
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/others.rs:111:16
--> tests/ui/borrow_interior_mutable_const/others.rs:98:16
|
LL | assert_eq!(CELL.get(), 6);
| ^^^^
|
= note: there is a compiler inserted borrow here
= help: assign this const to a local or static variable, and use the variable here
error: aborting due to 14 previous errors
error: aborting due to 15 previous errors

View file

@ -1,8 +1,8 @@
error: a `const` item should not be interior mutable
--> tests/ui/borrow_interior_mutable_const/projections.rs:27:1
--> tests/ui/borrow_interior_mutable_const/projections.rs:27:7
|
LL | const CELL: Assoc<u8> = UnsafeCell::new(0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^
|
= help: consider making this `Sync` so that it can go in a static item or using a `thread_local`
note: the lint level is defined here
@ -12,18 +12,18 @@ LL | #![deny(clippy::declare_interior_mutable_const)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: a `const` item should not be interior mutable
--> tests/ui/borrow_interior_mutable_const/projections.rs:29:1
--> tests/ui/borrow_interior_mutable_const/projections.rs:29:7
|
LL | const MUTABLE: MaybeMutable = MaybeMutable::Mutable(CELL);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^
|
= help: consider making this `Sync` so that it can go in a static item or using a `thread_local`
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/projections.rs:38:16
--> tests/ui/borrow_interior_mutable_const/projections.rs:38:15
|
LL | print_ref(&CELL);
| ^^^^
| ^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
note: the lint level is defined here
@ -33,10 +33,10 @@ LL | #![deny(clippy::borrow_interior_mutable_const)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/projections.rs:40:16
--> tests/ui/borrow_interior_mutable_const/projections.rs:40:15
|
LL | print_ref(&MUTABLE);
| ^^^^^^^
| ^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here

View file

@ -12,8 +12,7 @@ trait ConcreteTypes {
const STRING: String;
fn function() {
let _ = &Self::ATOMIC;
//~^ borrow_interior_mutable_const
let _ = &Self::ATOMIC; //~ borrow_interior_mutable_const
let _ = &Self::STRING;
}
}
@ -24,8 +23,7 @@ impl ConcreteTypes for u64 {
fn function() {
// Lint this again since implementers can choose not to borrow it.
let _ = &Self::ATOMIC;
//~^ borrow_interior_mutable_const
let _ = &Self::ATOMIC; //~ borrow_interior_mutable_const
let _ = &Self::STRING;
}
}
@ -50,8 +48,7 @@ impl<T: ConstDefault> GenericTypes<T, AtomicUsize> for Vec<T> {
fn function() {
let _ = &Self::TO_REMAIN_GENERIC;
let _ = &Self::TO_BE_CONCRETE;
//~^ borrow_interior_mutable_const
let _ = &Self::TO_BE_CONCRETE; //~ borrow_interior_mutable_const
}
}
@ -86,10 +83,8 @@ impl<T: ConstDefault> AssocTypes for Vec<T> {
fn function() {
let _ = &Self::TO_BE_FROZEN;
let _ = &Self::TO_BE_UNFROZEN;
//~^ borrow_interior_mutable_const
let _ = &Self::WRAPPED_TO_BE_UNFROZEN;
//~^ borrow_interior_mutable_const
let _ = &Self::TO_BE_UNFROZEN; //~ borrow_interior_mutable_const
let _ = &Self::WRAPPED_TO_BE_UNFROZEN; //~ borrow_interior_mutable_const
let _ = &Self::WRAPPED_TO_BE_GENERIC_PARAM;
}
}
@ -111,8 +106,7 @@ where
fn function() {
let _ = &Self::NOT_BOUNDED;
let _ = &Self::BOUNDED;
//~^ borrow_interior_mutable_const
let _ = &Self::BOUNDED; //~ borrow_interior_mutable_const
}
}
@ -125,8 +119,7 @@ where
fn function() {
let _ = &Self::NOT_BOUNDED;
let _ = &Self::BOUNDED;
//~^ borrow_interior_mutable_const
let _ = &Self::BOUNDED; //~ borrow_interior_mutable_const
}
}
@ -155,10 +148,8 @@ impl SelfType for AtomicUsize {
const WRAPPED_SELF: Option<Self> = Some(AtomicUsize::new(21));
fn function() {
let _ = &Self::SELF;
//~^ borrow_interior_mutable_const
let _ = &Self::WRAPPED_SELF;
//~^ borrow_interior_mutable_const
let _ = &Self::SELF; //~ borrow_interior_mutable_const
let _ = &Self::WRAPPED_SELF; //~ borrow_interior_mutable_const
}
}
@ -167,10 +158,8 @@ trait BothOfCellAndGeneric<T> {
const INDIRECT: Cell<*const T>;
fn function() {
let _ = &Self::DIRECT;
//~^ borrow_interior_mutable_const
let _ = &Self::INDIRECT;
//~^ borrow_interior_mutable_const
let _ = &Self::DIRECT; //~ borrow_interior_mutable_const
let _ = &Self::INDIRECT; //~ borrow_interior_mutable_const
}
}
@ -179,10 +168,8 @@ impl<T: ConstDefault> BothOfCellAndGeneric<T> for Vec<T> {
const INDIRECT: Cell<*const T> = Cell::new(std::ptr::null());
fn function() {
let _ = &Self::DIRECT;
//~^ borrow_interior_mutable_const
let _ = &Self::INDIRECT;
//~^ borrow_interior_mutable_const
let _ = &Self::DIRECT; //~ borrow_interior_mutable_const
let _ = &Self::INDIRECT; //~ borrow_interior_mutable_const
}
}
@ -201,19 +188,15 @@ where
const BOUNDED_ASSOC_TYPE: T::ToBeBounded = AtomicUsize::new(19);
fn function() {
let _ = &Self::ATOMIC;
//~^ borrow_interior_mutable_const
let _ = &Self::ATOMIC; //~ borrow_interior_mutable_const
let _ = &Self::COW;
let _ = &Self::GENERIC_TYPE;
let _ = &Self::ASSOC_TYPE;
let _ = &Self::BOUNDED_ASSOC_TYPE;
//~^ borrow_interior_mutable_const
let _ = &Self::BOUNDED_ASSOC_TYPE; //~ borrow_interior_mutable_const
}
}
fn main() {
u64::ATOMIC.store(5, Ordering::SeqCst);
//~^ borrow_interior_mutable_const
assert_eq!(u64::ATOMIC.load(Ordering::SeqCst), 9);
//~^ borrow_interior_mutable_const
u64::ATOMIC.store(5, Ordering::SeqCst); //~ borrow_interior_mutable_const
assert_eq!(u64::ATOMIC.load(Ordering::SeqCst), 9); //~ borrow_interior_mutable_const
}

View file

@ -1,8 +1,8 @@
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/traits.rs:15:18
--> tests/ui/borrow_interior_mutable_const/traits.rs:15:17
|
LL | let _ = &Self::ATOMIC;
| ^^^^^^^^^^^^
| ^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
note: the lint level is defined here
@ -12,131 +12,133 @@ LL | #![deny(clippy::borrow_interior_mutable_const)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/traits.rs:27:18
--> tests/ui/borrow_interior_mutable_const/traits.rs:26:17
|
LL | let _ = &Self::ATOMIC;
| ^^^^^^^^^^^^
| ^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/traits.rs:53:18
--> tests/ui/borrow_interior_mutable_const/traits.rs:51:17
|
LL | let _ = &Self::TO_BE_CONCRETE;
| ^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/traits.rs:89:18
--> tests/ui/borrow_interior_mutable_const/traits.rs:86:17
|
LL | let _ = &Self::TO_BE_UNFROZEN;
| ^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/traits.rs:91:18
--> tests/ui/borrow_interior_mutable_const/traits.rs:87:17
|
LL | let _ = &Self::WRAPPED_TO_BE_UNFROZEN;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/traits.rs:114:18
--> tests/ui/borrow_interior_mutable_const/traits.rs:109:17
|
LL | let _ = &Self::BOUNDED;
| ^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/traits.rs:128:18
--> tests/ui/borrow_interior_mutable_const/traits.rs:122:17
|
LL | let _ = &Self::BOUNDED;
| ^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/traits.rs:158:18
--> tests/ui/borrow_interior_mutable_const/traits.rs:151:17
|
LL | let _ = &Self::SELF;
| ^^^^^^^^^^
| ^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/traits.rs:160:18
--> tests/ui/borrow_interior_mutable_const/traits.rs:152:17
|
LL | let _ = &Self::WRAPPED_SELF;
| ^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/traits.rs:170:18
--> tests/ui/borrow_interior_mutable_const/traits.rs:161:17
|
LL | let _ = &Self::DIRECT;
| ^^^^^^^^^^^^
| ^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/traits.rs:172:18
--> tests/ui/borrow_interior_mutable_const/traits.rs:162:17
|
LL | let _ = &Self::INDIRECT;
| ^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/traits.rs:182:18
--> tests/ui/borrow_interior_mutable_const/traits.rs:171:17
|
LL | let _ = &Self::DIRECT;
| ^^^^^^^^^^^^
| ^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/traits.rs:184:18
--> tests/ui/borrow_interior_mutable_const/traits.rs:172:17
|
LL | let _ = &Self::INDIRECT;
| ^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/traits.rs:204:18
--> tests/ui/borrow_interior_mutable_const/traits.rs:191:17
|
LL | let _ = &Self::ATOMIC;
| ^^^^^^^^^^^^
| ^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/traits.rs:209:18
--> tests/ui/borrow_interior_mutable_const/traits.rs:195:17
|
LL | let _ = &Self::BOUNDED_ASSOC_TYPE;
| ^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/traits.rs:215:5
--> tests/ui/borrow_interior_mutable_const/traits.rs:200:5
|
LL | u64::ATOMIC.store(5, Ordering::SeqCst);
| ^^^^^^^^^^^
|
= note: there is a compiler inserted borrow here
= help: assign this const to a local or static variable, and use the variable here
error: a `const` item with interior mutability should not be borrowed
--> tests/ui/borrow_interior_mutable_const/traits.rs:217:16
--> tests/ui/borrow_interior_mutable_const/traits.rs:201:16
|
LL | assert_eq!(u64::ATOMIC.load(Ordering::SeqCst), 9);
| ^^^^^^^^^^^
|
= note: there is a compiler inserted borrow here
= help: assign this const to a local or static variable, and use the variable here
error: aborting due to 17 previous errors

View file

@ -0,0 +1,2 @@
#[deny(clippy::declare_interior_mutable_const)] //~ empty_line_after_outer_attr
const FOO: u8 = 0;

View file

@ -0,0 +1,3 @@
#![deny(clippy::declare_interior_mutable_const)] //~ empty_line_after_outer_attr
const FOO: u8 = 0;

View file

@ -0,0 +1,3 @@
#[deny(clippy::declare_interior_mutable_const)] //~ empty_line_after_outer_attr
const FOO: u8 = 0;

View file

@ -0,0 +1,19 @@
error: empty line after outer attribute
--> tests/ui/crashes/ice-12979.rs:1:1
|
LL | / #[deny(clippy::declare_interior_mutable_const)]
LL | |
| |_^
LL | const FOO: u8 = 0;
| --------- the attribute applies to this constant item
|
= note: `-D clippy::empty-line-after-outer-attr` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::empty_line_after_outer_attr)]`
= help: if the empty line is unintentional, remove it
help: if the attribute should apply to the crate use an inner attribute
|
LL | #![deny(clippy::declare_interior_mutable_const)]
| +
error: aborting due to 1 previous error

View file

@ -1,4 +0,0 @@
const UNINIT: core::mem::MaybeUninit<core::cell::Cell<&'static ()>> = core::mem::MaybeUninit::uninit();
//~^ declare_interior_mutable_const
fn main() {}

View file

@ -1,12 +0,0 @@
error: a `const` item should not be interior mutable
--> tests/ui/crashes/ice-9445.rs:1:1
|
LL | const UNINIT: core::mem::MaybeUninit<core::cell::Cell<&'static ()>> = core::mem::MaybeUninit::uninit();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider making this `Sync` so that it can go in a static item or using a `thread_local`
= note: `-D clippy::declare-interior-mutable-const` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::declare_interior_mutable_const)]`
error: aborting due to 1 previous error

View file

@ -9,8 +9,7 @@ enum OptionalCell {
}
// a constant with enums should be linted only when the used variant is unfrozen (#3962).
const UNFROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(true));
//~^ declare_interior_mutable_const
const UNFROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(true)); //~ declare_interior_mutable_const
const FROZEN_VARIANT: OptionalCell = OptionalCell::Frozen;
const fn unfrozen_variant() -> OptionalCell {
@ -21,8 +20,7 @@ const fn frozen_variant() -> OptionalCell {
OptionalCell::Frozen
}
const UNFROZEN_VARIANT_FROM_FN: OptionalCell = unfrozen_variant();
//~^ declare_interior_mutable_const
const UNFROZEN_VARIANT_FROM_FN: OptionalCell = unfrozen_variant(); //~ declare_interior_mutable_const
const FROZEN_VARIANT_FROM_FN: OptionalCell = frozen_variant();
enum NestedInnermost {
@ -60,24 +58,21 @@ trait AssocConsts {
// When there's no default value, lint it only according to its type.
// Further details are on the corresponding code (`NonCopyConst::check_trait_item`).
const TO_BE_UNFROZEN_VARIANT: OptionalCell;
//~^ declare_interior_mutable_const
const TO_BE_FROZEN_VARIANT: OptionalCell;
//~^ declare_interior_mutable_const
// Lint default values accordingly.
const DEFAULTED_ON_UNFROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(false));
//~^ declare_interior_mutable_const
const DEFAULTED_ON_UNFROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(false)); //~ declare_interior_mutable_const
const DEFAULTED_ON_FROZEN_VARIANT: OptionalCell = OptionalCell::Frozen;
}
// The lint doesn't trigger for an assoc constant in a trait impl with an unfrozen type even if it
// has enums. Further details are on the corresponding code in 'NonCopyConst::check_impl_item'.
impl AssocConsts for u64 {
const TO_BE_UNFROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(false));
const TO_BE_UNFROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(false)); //~ declare_interior_mutable_const
const TO_BE_FROZEN_VARIANT: OptionalCell = OptionalCell::Frozen;
// even if this sets an unfrozen variant, the lint ignores it.
const DEFAULTED_ON_FROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(false));
const DEFAULTED_ON_FROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(false)); //~ declare_interior_mutable_const
}
// At first, I thought I'd need to check every patterns in `trait.rs`; but, what matters
@ -92,8 +87,7 @@ trait AssocTypes {
impl AssocTypes for u64 {
type ToBeUnfrozen = AtomicUsize;
const TO_BE_UNFROZEN_VARIANT: Option<Self::ToBeUnfrozen> = Some(Self::ToBeUnfrozen::new(4));
//~^ declare_interior_mutable_const
const TO_BE_UNFROZEN_VARIANT: Option<Self::ToBeUnfrozen> = Some(Self::ToBeUnfrozen::new(4)); //~ declare_interior_mutable_const
const TO_BE_FROZEN_VARIANT: Option<Self::ToBeUnfrozen> = None;
}
@ -105,30 +99,25 @@ enum BothOfCellAndGeneric<T> {
}
impl<T> BothOfCellAndGeneric<T> {
const UNFROZEN_VARIANT: BothOfCellAndGeneric<T> = BothOfCellAndGeneric::Unfrozen(Cell::new(std::ptr::null()));
//~^ declare_interior_mutable_const
const UNFROZEN_VARIANT: BothOfCellAndGeneric<T> = BothOfCellAndGeneric::Unfrozen(Cell::new(std::ptr::null())); //~ declare_interior_mutable_const
// This is a false positive. The argument about this is on `is_value_unfrozen_raw`
const GENERIC_VARIANT: BothOfCellAndGeneric<T> = BothOfCellAndGeneric::Generic(std::ptr::null());
//~^ declare_interior_mutable_const
const FROZEN_VARIANT: BothOfCellAndGeneric<T> = BothOfCellAndGeneric::Frozen(5);
// This is what is likely to be a false negative when one tries to fix
// the `GENERIC_VARIANT` false positive.
const NO_ENUM: Cell<*const T> = Cell::new(std::ptr::null());
//~^ declare_interior_mutable_const
const NO_ENUM: Cell<*const T> = Cell::new(std::ptr::null()); //~ declare_interior_mutable_const
}
// associated types here is basically the same as the one above.
trait BothOfCellAndGenericWithAssocType {
type AssocType;
const UNFROZEN_VARIANT: BothOfCellAndGeneric<Self::AssocType> =
//~^ declare_interior_mutable_const
const UNFROZEN_VARIANT: BothOfCellAndGeneric<Self::AssocType> = //~ declare_interior_mutable_const
BothOfCellAndGeneric::Unfrozen(Cell::new(std::ptr::null()));
const GENERIC_VARIANT: BothOfCellAndGeneric<Self::AssocType> = BothOfCellAndGeneric::Generic(std::ptr::null());
//~^ declare_interior_mutable_const
const FROZEN_VARIANT: BothOfCellAndGeneric<Self::AssocType> = BothOfCellAndGeneric::Frozen(5);
}

View file

@ -1,89 +1,70 @@
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/enums.rs:12:1
--> tests/ui/declare_interior_mutable_const/enums.rs:12:7
|
LL | const UNFROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(true));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^
|
= help: consider making this `Sync` so that it can go in a static item or using a `thread_local`
= note: `-D clippy::declare-interior-mutable-const` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::declare_interior_mutable_const)]`
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/enums.rs:24:1
--> tests/ui/declare_interior_mutable_const/enums.rs:23:7
|
LL | const UNFROZEN_VARIANT_FROM_FN: OptionalCell = unfrozen_variant();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider making this `Sync` so that it can go in a static item or using a `thread_local`
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/enums.rs:47:1
--> tests/ui/declare_interior_mutable_const/enums.rs:45:7
|
LL | / const NESTED_UNFROZEN_VARIANT: NestedOutermost = NestedOutermost {
LL | |
LL | | outer: NestedOuter::NestedInner(NestedInner {
LL | | inner: NestedInnermost::Unfrozen(AtomicUsize::new(2)),
LL | | }),
LL | | };
| |__^
LL | const NESTED_UNFROZEN_VARIANT: NestedOutermost = NestedOutermost {
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider making this a static item
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/enums.rs:62:5
|
LL | const TO_BE_UNFROZEN_VARIANT: OptionalCell;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/enums.rs:64:5
|
LL | const TO_BE_FROZEN_VARIANT: OptionalCell;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/enums.rs:68:5
--> tests/ui/declare_interior_mutable_const/enums.rs:64:11
|
LL | const DEFAULTED_ON_UNFROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(false));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/enums.rs:95:5
--> tests/ui/declare_interior_mutable_const/enums.rs:71:11
|
LL | const TO_BE_UNFROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(false));
| ^^^^^^^^^^^^^^^^^^^^^^
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/enums.rs:75:11
|
LL | const DEFAULTED_ON_FROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(false));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/enums.rs:90:11
|
LL | const TO_BE_UNFROZEN_VARIANT: Option<Self::ToBeUnfrozen> = Some(Self::ToBeUnfrozen::new(4));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/enums.rs:108:5
--> tests/ui/declare_interior_mutable_const/enums.rs:102:11
|
LL | const UNFROZEN_VARIANT: BothOfCellAndGeneric<T> = BothOfCellAndGeneric::Unfrozen(Cell::new(std::ptr::null()));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/enums.rs:112:5
|
LL | const GENERIC_VARIANT: BothOfCellAndGeneric<T> = BothOfCellAndGeneric::Generic(std::ptr::null());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/enums.rs:119:5
--> tests/ui/declare_interior_mutable_const/enums.rs:111:11
|
LL | const NO_ENUM: Cell<*const T> = Cell::new(std::ptr::null());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/enums.rs:127:5
--> tests/ui/declare_interior_mutable_const/enums.rs:118:11
|
LL | / const UNFROZEN_VARIANT: BothOfCellAndGeneric<Self::AssocType> =
LL | |
LL | | BothOfCellAndGeneric::Unfrozen(Cell::new(std::ptr::null()));
| |____________________________________________________________________^
LL | const UNFROZEN_VARIANT: BothOfCellAndGeneric<Self::AssocType> =
| ^^^^^^^^^^^^^^^^
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/enums.rs:130:5
|
LL | const GENERIC_VARIANT: BothOfCellAndGeneric<Self::AssocType> = BothOfCellAndGeneric::Generic(std::ptr::null());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 12 previous errors
error: aborting due to 10 previous errors

View file

@ -7,20 +7,17 @@ use std::ptr;
use std::sync::Once;
use std::sync::atomic::AtomicUsize;
const ATOMIC: AtomicUsize = AtomicUsize::new(5);
//~^ declare_interior_mutable_const
const CELL: Cell<usize> = Cell::new(6);
//~^ declare_interior_mutable_const
const ATOMIC: AtomicUsize = AtomicUsize::new(5); //~ declare_interior_mutable_const
const CELL: Cell<usize> = Cell::new(6); //~ declare_interior_mutable_const
const ATOMIC_TUPLE: ([AtomicUsize; 1], Vec<AtomicUsize>, u8) = ([ATOMIC], Vec::new(), 7);
//~^ declare_interior_mutable_const
macro_rules! declare_const {
($name:ident: $ty:ty = $e:expr) => {
const $name: $ty = $e;
//~^ declare_interior_mutable_const
};
}
declare_const!(_ONCE: Once = Once::new());
declare_const!(_ONCE: Once = Once::new()); //~ declare_interior_mutable_const
// const ATOMIC_REF: &AtomicUsize = &AtomicUsize::new(7); // This will simply trigger E0492.

View file

@ -1,49 +1,47 @@
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/others.rs:10:1
--> tests/ui/declare_interior_mutable_const/others.rs:10:7
|
LL | const ATOMIC: AtomicUsize = AtomicUsize::new(5);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^
|
= help: consider making this a static item
= note: `-D clippy::declare-interior-mutable-const` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::declare_interior_mutable_const)]`
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/others.rs:12:1
--> tests/ui/declare_interior_mutable_const/others.rs:11:7
|
LL | const CELL: Cell<usize> = Cell::new(6);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^
|
= help: consider making this `Sync` so that it can go in a static item or using a `thread_local`
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/others.rs:14:1
--> tests/ui/declare_interior_mutable_const/others.rs:12:7
|
LL | const ATOMIC_TUPLE: ([AtomicUsize; 1], Vec<AtomicUsize>, u8) = ([ATOMIC], Vec::new(), 7);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^
|
= help: consider making this a static item
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/others.rs:19:9
--> tests/ui/declare_interior_mutable_const/others.rs:20:16
|
LL | const $name: $ty = $e;
| ^^^^^^^^^^^^^^^^^^^^^^
...
LL | declare_const!(_ONCE: Once = Once::new());
| ----------------------------------------- in this macro invocation
| ^^^^^
|
= note: this error originates in the macro `declare_const` (in Nightly builds, run with -Z macro-backtrace for more info)
= help: consider making this a static item
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/others.rs:47:13
--> tests/ui/declare_interior_mutable_const/others.rs:44:19
|
LL | const _BAZ: Cell<usize> = Cell::new(0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^
...
LL | issue_8493!();
| ------------- in this macro invocation
|
= help: consider making this `Sync` so that it can go in a static item or using a `thread_local`
= note: this error originates in the macro `issue_8493` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 5 previous errors

View file

@ -7,17 +7,15 @@ use std::sync::atomic::AtomicUsize;
macro_rules! declare_const {
($name:ident: $ty:ty = $e:expr) => {
const $name: $ty = $e;
//~^ declare_interior_mutable_const
};
}
// a constant whose type is a concrete type should be linted at the definition site.
trait ConcreteTypes {
const ATOMIC: AtomicUsize;
//~^ declare_interior_mutable_const
const ATOMIC: AtomicUsize; //~ declare_interior_mutable_const
const INTEGER: u64;
const STRING: String;
declare_const!(ANOTHER_ATOMIC: AtomicUsize = Self::ATOMIC);
declare_const!(ANOTHER_ATOMIC: AtomicUsize = Self::ATOMIC); //~ declare_interior_mutable_const
}
impl ConcreteTypes for u64 {
@ -43,7 +41,6 @@ trait GenericTypes<T, U> {
impl<T: ConstDefault> GenericTypes<T, AtomicUsize> for u64 {
const TO_REMAIN_GENERIC: T = T::DEFAULT;
const TO_BE_CONCRETE: AtomicUsize = AtomicUsize::new(11);
//~^ declare_interior_mutable_const
}
// a helper type used below
@ -68,10 +65,8 @@ impl<T: ConstDefault> AssocTypes for Vec<T> {
type ToBeGenericParam = T;
const TO_BE_FROZEN: Self::ToBeFrozen = 12;
const TO_BE_UNFROZEN: Self::ToBeUnfrozen = AtomicUsize::new(13);
//~^ declare_interior_mutable_const
const WRAPPED_TO_BE_UNFROZEN: Wrapper<Self::ToBeUnfrozen> = Wrapper(AtomicUsize::new(14));
//~^ declare_interior_mutable_const
const TO_BE_UNFROZEN: Self::ToBeUnfrozen = AtomicUsize::new(13); //~ declare_interior_mutable_const
const WRAPPED_TO_BE_UNFROZEN: Wrapper<Self::ToBeUnfrozen> = Wrapper(AtomicUsize::new(14)); //~ declare_interior_mutable_const
const WRAPPED_TO_BE_GENERIC_PARAM: Wrapper<Self::ToBeGenericParam> = Wrapper(T::DEFAULT);
}
@ -90,8 +85,7 @@ where
T: AssocTypesHelper<ToBeBounded = AtomicUsize>,
{
const NOT_BOUNDED: T::NotToBeBounded;
const BOUNDED: T::ToBeBounded;
//~^ declare_interior_mutable_const
const BOUNDED: T::ToBeBounded; //~ declare_interior_mutable_const
}
impl<T> AssocTypesFromGenericParam<T> for u64
@ -120,23 +114,18 @@ impl SelfType for AtomicUsize {
// this (interior mutable `Self` const) exists in `parking_lot`.
// `const_trait_impl` will replace it in the future, hopefully.
const SELF: Self = AtomicUsize::new(17);
//~^ declare_interior_mutable_const
const WRAPPED_SELF: Option<Self> = Some(AtomicUsize::new(21));
//~^ declare_interior_mutable_const
const WRAPPED_SELF: Option<Self> = Some(AtomicUsize::new(21)); //~ declare_interior_mutable_const
}
// Even though a constant contains a generic type, if it also have an interior mutable type,
// it should be linted at the definition site.
trait BothOfCellAndGeneric<T> {
const DIRECT: Cell<T>;
//~^ declare_interior_mutable_const
const INDIRECT: Cell<*const T>;
//~^ declare_interior_mutable_const
const DIRECT: Cell<T>; //~ declare_interior_mutable_const
const INDIRECT: Cell<*const T>; //~ declare_interior_mutable_const
}
impl<T: ConstDefault> BothOfCellAndGeneric<T> for u64 {
const DIRECT: Cell<T> = Cell::new(T::DEFAULT);
//~^ declare_interior_mutable_const
const INDIRECT: Cell<*const T> = Cell::new(std::ptr::null());
}
@ -148,15 +137,13 @@ impl<T> Local<T>
where
T: ConstDefault + AssocTypesHelper<ToBeBounded = AtomicUsize>,
{
const ATOMIC: AtomicUsize = AtomicUsize::new(18);
//~^ declare_interior_mutable_const
const ATOMIC: AtomicUsize = AtomicUsize::new(18); //~ declare_interior_mutable_const
const COW: Cow<'static, str> = Cow::Borrowed("tuvwxy");
const GENERIC_TYPE: T = T::DEFAULT;
const ASSOC_TYPE: T::NotToBeBounded = T::NOT_TO_BE_BOUNDED;
const BOUNDED_ASSOC_TYPE: T::ToBeBounded = AtomicUsize::new(19);
//~^ declare_interior_mutable_const
const BOUNDED_ASSOC_TYPE: T::ToBeBounded = AtomicUsize::new(19); //~ declare_interior_mutable_const
}
fn main() {}

View file

@ -1,88 +1,65 @@
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/traits.rs:16:5
--> tests/ui/declare_interior_mutable_const/traits.rs:15:11
|
LL | const ATOMIC: AtomicUsize;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^
|
= note: `-D clippy::declare-interior-mutable-const` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::declare_interior_mutable_const)]`
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/traits.rs:9:9
--> tests/ui/declare_interior_mutable_const/traits.rs:18:20
|
LL | const $name: $ty = $e;
| ^^^^^^^^^^^^^^^^^^^^^^
...
LL | declare_const!(ANOTHER_ATOMIC: AtomicUsize = Self::ATOMIC);
| ---------------------------------------------------------- in this macro invocation
|
= note: this error originates in the macro `declare_const` (in Nightly builds, run with -Z macro-backtrace for more info)
| ^^^^^^^^^^^^^^
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/traits.rs:45:5
|
LL | const TO_BE_CONCRETE: AtomicUsize = AtomicUsize::new(11);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/traits.rs:71:5
--> tests/ui/declare_interior_mutable_const/traits.rs:68:11
|
LL | const TO_BE_UNFROZEN: Self::ToBeUnfrozen = AtomicUsize::new(13);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/traits.rs:73:5
--> tests/ui/declare_interior_mutable_const/traits.rs:69:11
|
LL | const WRAPPED_TO_BE_UNFROZEN: Wrapper<Self::ToBeUnfrozen> = Wrapper(AtomicUsize::new(14));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/traits.rs:93:5
--> tests/ui/declare_interior_mutable_const/traits.rs:88:11
|
LL | const BOUNDED: T::ToBeBounded;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/traits.rs:122:5
|
LL | const SELF: Self = AtomicUsize::new(17);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/traits.rs:124:5
--> tests/ui/declare_interior_mutable_const/traits.rs:117:11
|
LL | const WRAPPED_SELF: Option<Self> = Some(AtomicUsize::new(21));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/traits.rs:131:5
--> tests/ui/declare_interior_mutable_const/traits.rs:123:11
|
LL | const DIRECT: Cell<T>;
| ^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/traits.rs:133:5
--> tests/ui/declare_interior_mutable_const/traits.rs:124:11
|
LL | const INDIRECT: Cell<*const T>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/traits.rs:138:5
|
LL | const DIRECT: Cell<T> = Cell::new(T::DEFAULT);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/traits.rs:151:5
--> tests/ui/declare_interior_mutable_const/traits.rs:140:11
|
LL | const ATOMIC: AtomicUsize = AtomicUsize::new(18);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/traits.rs:158:5
--> tests/ui/declare_interior_mutable_const/traits.rs:146:11
|
LL | const BOUNDED_ASSOC_TYPE: T::ToBeBounded = AtomicUsize::new(19);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^
error: aborting due to 13 previous errors
error: aborting due to 10 previous errors