Skip to content

Commit aecbb28

Browse files
committed
Rewrite non_copy_const
1 parent 3474d6a commit aecbb28

File tree

16 files changed

+917
-604
lines changed

16 files changed

+917
-604
lines changed

clippy_lints/src/non_copy_const.rs

+698-326
Large diffs are not rendered by default.

clippy_utils/src/ty.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1340,7 +1340,7 @@ pub fn get_adt_inherent_method<'a>(cx: &'a LateContext<'_>, ty: Ty<'_>, method_n
13401340
}
13411341
}
13421342

1343-
/// Get's the type of a field by name.
1343+
/// Gets the type of a field by name.
13441344
pub fn get_field_by_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, name: Symbol) -> Option<Ty<'tcx>> {
13451345
match *ty.kind() {
13461346
ty::Adt(def, args) if def.is_union() || def.is_struct() => def
@@ -1353,3 +1353,14 @@ pub fn get_field_by_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, name: Symbol) ->
13531353
_ => None,
13541354
}
13551355
}
1356+
1357+
/// Gets the index of a field by name.
1358+
pub fn get_field_idx_by_name(ty: Ty<'_>, name: Symbol) -> Option<usize> {
1359+
match *ty.kind() {
1360+
ty::Adt(def, _) if def.is_union() || def.is_struct() => {
1361+
def.non_enum_variant().fields.iter().position(|f| f.name == name)
1362+
},
1363+
ty::Tuple(_) => name.as_str().parse::<usize>().ok(),
1364+
_ => None,
1365+
}
1366+
}

tests/ui/borrow_interior_mutable_const/enums.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const UNFROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(true));
1919
const FROZEN_VARIANT: OptionalCell = OptionalCell::Frozen;
2020

2121
fn borrow_optional_cell() {
22-
let _ = &UNFROZEN_VARIANT; //~ ERROR: interior mutability
22+
let _ = &UNFROZEN_VARIANT; //~ borrow_interior_mutable_const
2323
let _ = &FROZEN_VARIANT;
2424
}
2525

@@ -34,11 +34,11 @@ trait AssocConsts {
3434
// This is the "suboptimal behavior" mentioned in `is_value_unfrozen`
3535
// caused by a similar reason to unfrozen types without any default values
3636
// get linted even if it has frozen variants'.
37-
let _ = &Self::TO_BE_FROZEN_VARIANT; //~ ERROR: interior mutability
37+
let _ = &Self::TO_BE_FROZEN_VARIANT;
3838

3939
// The lint ignores default values because an impl of this trait can set
4040
// an unfrozen variant to `DEFAULTED_ON_FROZEN_VARIANT` and use the default impl for `function`.
41-
let _ = &Self::DEFAULTED_ON_FROZEN_VARIANT; //~ ERROR: interior mutability
41+
let _ = &Self::DEFAULTED_ON_FROZEN_VARIANT;
4242
}
4343
}
4444

@@ -47,9 +47,9 @@ impl AssocConsts for u64 {
4747
const TO_BE_FROZEN_VARIANT: OptionalCell = OptionalCell::Frozen;
4848

4949
fn function() {
50-
let _ = &<Self as AssocConsts>::TO_BE_UNFROZEN_VARIANT; //~ ERROR: interior mutability
50+
let _ = &<Self as AssocConsts>::TO_BE_UNFROZEN_VARIANT; //~ borrow_interior_mutable_const
5151
let _ = &<Self as AssocConsts>::TO_BE_FROZEN_VARIANT;
52-
let _ = &Self::DEFAULTED_ON_UNFROZEN_VARIANT; //~ ERROR: interior mutability
52+
let _ = &Self::DEFAULTED_ON_UNFROZEN_VARIANT; //~ borrow_interior_mutable_const
5353
let _ = &Self::DEFAULTED_ON_FROZEN_VARIANT;
5454
}
5555
}
@@ -71,7 +71,7 @@ impl AssocTypes for u64 {
7171
const TO_BE_FROZEN_VARIANT: Option<Self::ToBeUnfrozen> = None;
7272

7373
fn function() {
74-
let _ = &<Self as AssocTypes>::TO_BE_UNFROZEN_VARIANT; //~ ERROR: interior mutability
74+
let _ = &<Self as AssocTypes>::TO_BE_UNFROZEN_VARIANT; //~ borrow_interior_mutable_const
7575
let _ = &<Self as AssocTypes>::TO_BE_FROZEN_VARIANT;
7676
}
7777
}
@@ -88,14 +88,14 @@ impl<T> BothOfCellAndGeneric<T> {
8888
const FROZEN_VARIANT: BothOfCellAndGeneric<T> = BothOfCellAndGeneric::Frozen(5);
8989

9090
fn function() {
91-
let _ = &Self::UNFROZEN_VARIANT; //~ ERROR: interior mutability
92-
let _ = &Self::GENERIC_VARIANT; //~ ERROR: interior mutability
91+
let _ = &Self::UNFROZEN_VARIANT; //~ borrow_interior_mutable_const
92+
let _ = &Self::GENERIC_VARIANT;
9393
let _ = &Self::FROZEN_VARIANT;
9494
}
9595
}
9696

9797
fn main() {
9898
// constants defined in foreign crates
99-
let _ = &helper::WRAPPED_PRIVATE_UNFROZEN_VARIANT; //~ ERROR: interior mutability
99+
let _ = &helper::WRAPPED_PRIVATE_UNFROZEN_VARIANT; //~ borrow_interior_mutable_const
100100
let _ = &helper::WRAPPED_PRIVATE_FROZEN_VARIANT;
101101
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: a `const` item with interior mutability should not be borrowed
2-
--> tests/ui/borrow_interior_mutable_const/enums.rs:22:14
2+
--> tests/ui/borrow_interior_mutable_const/enums.rs:22:13
33
|
44
LL | let _ = &UNFROZEN_VARIANT;
5-
| ^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^
66
|
77
= help: assign this const to a local or static variable, and use the variable here
88
note: the lint level is defined here
@@ -12,68 +12,44 @@ LL | #![deny(clippy::borrow_interior_mutable_const)]
1212
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1313

1414
error: a `const` item with interior mutability should not be borrowed
15-
--> tests/ui/borrow_interior_mutable_const/enums.rs:37:18
16-
|
17-
LL | let _ = &Self::TO_BE_FROZEN_VARIANT;
18-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
19-
|
20-
= help: assign this const to a local or static variable, and use the variable here
21-
22-
error: a `const` item with interior mutability should not be borrowed
23-
--> tests/ui/borrow_interior_mutable_const/enums.rs:41:18
24-
|
25-
LL | let _ = &Self::DEFAULTED_ON_FROZEN_VARIANT;
26-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
27-
|
28-
= help: assign this const to a local or static variable, and use the variable here
29-
30-
error: a `const` item with interior mutability should not be borrowed
31-
--> tests/ui/borrow_interior_mutable_const/enums.rs:50:18
15+
--> tests/ui/borrow_interior_mutable_const/enums.rs:50:17
3216
|
3317
LL | let _ = &<Self as AssocConsts>::TO_BE_UNFROZEN_VARIANT;
34-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3519
|
3620
= help: assign this const to a local or static variable, and use the variable here
3721

3822
error: a `const` item with interior mutability should not be borrowed
39-
--> tests/ui/borrow_interior_mutable_const/enums.rs:52:18
23+
--> tests/ui/borrow_interior_mutable_const/enums.rs:52:17
4024
|
4125
LL | let _ = &Self::DEFAULTED_ON_UNFROZEN_VARIANT;
42-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
26+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4327
|
4428
= help: assign this const to a local or static variable, and use the variable here
4529

4630
error: a `const` item with interior mutability should not be borrowed
47-
--> tests/ui/borrow_interior_mutable_const/enums.rs:74:18
31+
--> tests/ui/borrow_interior_mutable_const/enums.rs:74:17
4832
|
4933
LL | let _ = &<Self as AssocTypes>::TO_BE_UNFROZEN_VARIANT;
50-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
34+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5135
|
5236
= help: assign this const to a local or static variable, and use the variable here
5337

5438
error: a `const` item with interior mutability should not be borrowed
55-
--> tests/ui/borrow_interior_mutable_const/enums.rs:91:18
39+
--> tests/ui/borrow_interior_mutable_const/enums.rs:91:17
5640
|
5741
LL | let _ = &Self::UNFROZEN_VARIANT;
58-
| ^^^^^^^^^^^^^^^^^^^^^^
59-
|
60-
= help: assign this const to a local or static variable, and use the variable here
61-
62-
error: a `const` item with interior mutability should not be borrowed
63-
--> tests/ui/borrow_interior_mutable_const/enums.rs:92:18
64-
|
65-
LL | let _ = &Self::GENERIC_VARIANT;
66-
| ^^^^^^^^^^^^^^^^^^^^^
42+
| ^^^^^^^^^^^^^^^^^^^^^^^
6743
|
6844
= help: assign this const to a local or static variable, and use the variable here
6945

7046
error: a `const` item with interior mutability should not be borrowed
71-
--> tests/ui/borrow_interior_mutable_const/enums.rs:99:14
47+
--> tests/ui/borrow_interior_mutable_const/enums.rs:99:13
7248
|
7349
LL | let _ = &helper::WRAPPED_PRIVATE_UNFROZEN_VARIANT;
74-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
50+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7551
|
7652
= help: assign this const to a local or static variable, and use the variable here
7753

78-
error: aborting due to 9 previous errors
54+
error: aborting due to 6 previous errors
7955

tests/ui/borrow_interior_mutable_const/others.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ impl<T> std::ops::Deref for StaticRef<T> {
5151
const CELL_REF: StaticRef<(UnsafeCell<u32>,)> = unsafe { StaticRef::new(std::ptr::null()) };
5252

5353
fn main() {
54-
ATOMIC.store(1, Ordering::SeqCst); //~ ERROR: interior mutability
55-
assert_eq!(ATOMIC.load(Ordering::SeqCst), 5); //~ ERROR: interior mutability
54+
ATOMIC.store(1, Ordering::SeqCst); //~ borrow_interior_mutable_const
55+
assert_eq!(ATOMIC.load(Ordering::SeqCst), 5); //~ borrow_interior_mutable_const
5656

5757
let _once = ONCE_INIT;
58-
let _once_ref = &ONCE_INIT; //~ ERROR: interior mutability
59-
let _once_ref_2 = &&ONCE_INIT; //~ ERROR: interior mutability
60-
let _once_ref_4 = &&&&ONCE_INIT; //~ ERROR: interior mutability
61-
let _once_mut = &mut ONCE_INIT; //~ ERROR: interior mutability
58+
let _once_ref = &ONCE_INIT; //~ borrow_interior_mutable_const
59+
let _once_ref_2 = &&ONCE_INIT; //~ borrow_interior_mutable_const
60+
let _once_ref_4 = &&&&ONCE_INIT; //~ borrow_interior_mutable_const
61+
let _once_mut = &mut ONCE_INIT; //~ borrow_interior_mutable_const
6262
let _atomic_into_inner = ATOMIC.into_inner();
6363
// these should be all fine.
6464
let _twice = (ONCE_INIT, ONCE_INIT);
@@ -69,23 +69,23 @@ fn main() {
6969
let _ref_array_once = &[ONCE_INIT, ONCE_INIT][0];
7070

7171
// referencing projection is still bad.
72-
let _ = &ATOMIC_TUPLE; //~ ERROR: interior mutability
73-
let _ = &ATOMIC_TUPLE.0; //~ ERROR: interior mutability
74-
let _ = &(&&&&ATOMIC_TUPLE).0; //~ ERROR: interior mutability
75-
let _ = &ATOMIC_TUPLE.0[0]; //~ ERROR: interior mutability
76-
let _ = ATOMIC_TUPLE.0[0].load(Ordering::SeqCst); //~ ERROR: interior mutability
72+
let _ = &ATOMIC_TUPLE; //~ borrow_interior_mutable_const
73+
let _ = &ATOMIC_TUPLE.0; //~ borrow_interior_mutable_const
74+
let _ = &(&&&&ATOMIC_TUPLE).0; //~ borrow_interior_mutable_const
75+
let _ = &ATOMIC_TUPLE.0[0]; //~ borrow_interior_mutable_const
76+
let _ = ATOMIC_TUPLE.0[0].load(Ordering::SeqCst); //~ borrow_interior_mutable_const
7777
let _ = &*ATOMIC_TUPLE.1;
7878
let _ = &ATOMIC_TUPLE.2;
79-
let _ = (&&&&ATOMIC_TUPLE).0;
80-
let _ = (&&&&ATOMIC_TUPLE).2;
79+
let _ = (&&&&ATOMIC_TUPLE).0; //~ borrow_interior_mutable_const
80+
let _ = (&&&&ATOMIC_TUPLE).2; //~ borrow_interior_mutable_const
8181
let _ = ATOMIC_TUPLE.0;
82-
let _ = ATOMIC_TUPLE.0[0]; //~ ERROR: interior mutability
82+
let _ = ATOMIC_TUPLE.0[0];
8383
let _ = ATOMIC_TUPLE.1.into_iter();
8484
let _ = ATOMIC_TUPLE.2;
8585
let _ = &{ ATOMIC_TUPLE };
8686

87-
CELL.set(2); //~ ERROR: interior mutability
88-
assert_eq!(CELL.get(), 6); //~ ERROR: interior mutability
87+
CELL.set(2); //~ borrow_interior_mutable_const
88+
assert_eq!(CELL.get(), 6); //~ borrow_interior_mutable_const
8989

9090
assert_eq!(INTEGER, 8);
9191
assert!(STRING.is_empty());

tests/ui/borrow_interior_mutable_const/others.stderr

+29-21
Original file line numberDiff line numberDiff line change
@@ -20,82 +20,90 @@ LL | assert_eq!(ATOMIC.load(Ordering::SeqCst), 5);
2020
= help: assign this const to a local or static variable, and use the variable here
2121

2222
error: a `const` item with interior mutability should not be borrowed
23-
--> tests/ui/borrow_interior_mutable_const/others.rs:58:22
23+
--> tests/ui/borrow_interior_mutable_const/others.rs:58:21
2424
|
2525
LL | let _once_ref = &ONCE_INIT;
26-
| ^^^^^^^^^
26+
| ^^^^^^^^^^
2727
|
2828
= help: assign this const to a local or static variable, and use the variable here
2929

3030
error: a `const` item with interior mutability should not be borrowed
31-
--> tests/ui/borrow_interior_mutable_const/others.rs:59:25
31+
--> tests/ui/borrow_interior_mutable_const/others.rs:59:24
3232
|
3333
LL | let _once_ref_2 = &&ONCE_INIT;
34-
| ^^^^^^^^^
34+
| ^^^^^^^^^^
3535
|
3636
= help: assign this const to a local or static variable, and use the variable here
3737

3838
error: a `const` item with interior mutability should not be borrowed
39-
--> tests/ui/borrow_interior_mutable_const/others.rs:60:27
39+
--> tests/ui/borrow_interior_mutable_const/others.rs:60:26
4040
|
4141
LL | let _once_ref_4 = &&&&ONCE_INIT;
42-
| ^^^^^^^^^
42+
| ^^^^^^^^^^
4343
|
4444
= help: assign this const to a local or static variable, and use the variable here
4545

4646
error: a `const` item with interior mutability should not be borrowed
47-
--> tests/ui/borrow_interior_mutable_const/others.rs:61:26
47+
--> tests/ui/borrow_interior_mutable_const/others.rs:61:21
4848
|
4949
LL | let _once_mut = &mut ONCE_INIT;
50-
| ^^^^^^^^^
50+
| ^^^^^^^^^^^^^^
5151
|
5252
= help: assign this const to a local or static variable, and use the variable here
5353

5454
error: a `const` item with interior mutability should not be borrowed
55-
--> tests/ui/borrow_interior_mutable_const/others.rs:72:14
55+
--> tests/ui/borrow_interior_mutable_const/others.rs:72:13
5656
|
5757
LL | let _ = &ATOMIC_TUPLE;
58-
| ^^^^^^^^^^^^
58+
| ^^^^^^^^^^^^^
5959
|
6060
= help: assign this const to a local or static variable, and use the variable here
6161

6262
error: a `const` item with interior mutability should not be borrowed
63-
--> tests/ui/borrow_interior_mutable_const/others.rs:73:14
63+
--> tests/ui/borrow_interior_mutable_const/others.rs:73:13
6464
|
6565
LL | let _ = &ATOMIC_TUPLE.0;
66-
| ^^^^^^^^^^^^
66+
| ^^^^^^^^^^^^^^^
6767
|
6868
= help: assign this const to a local or static variable, and use the variable here
6969

7070
error: a `const` item with interior mutability should not be borrowed
71-
--> tests/ui/borrow_interior_mutable_const/others.rs:74:19
71+
--> tests/ui/borrow_interior_mutable_const/others.rs:74:18
7272
|
7373
LL | let _ = &(&&&&ATOMIC_TUPLE).0;
74-
| ^^^^^^^^^^^^
74+
| ^^^^^^^^^^^^^
7575
|
7676
= help: assign this const to a local or static variable, and use the variable here
7777

7878
error: a `const` item with interior mutability should not be borrowed
79-
--> tests/ui/borrow_interior_mutable_const/others.rs:75:14
79+
--> tests/ui/borrow_interior_mutable_const/others.rs:75:13
8080
|
8181
LL | let _ = &ATOMIC_TUPLE.0[0];
82-
| ^^^^^^^^^^^^
82+
| ^^^^^^^^^^^^^^^^^^
8383
|
8484
= help: assign this const to a local or static variable, and use the variable here
8585

8686
error: a `const` item with interior mutability should not be borrowed
8787
--> tests/ui/borrow_interior_mutable_const/others.rs:76:13
8888
|
8989
LL | let _ = ATOMIC_TUPLE.0[0].load(Ordering::SeqCst);
90-
| ^^^^^^^^^^^^
90+
| ^^^^^^^^^^^^^^^^^
9191
|
9292
= help: assign this const to a local or static variable, and use the variable here
9393

9494
error: a `const` item with interior mutability should not be borrowed
95-
--> tests/ui/borrow_interior_mutable_const/others.rs:82:13
95+
--> tests/ui/borrow_interior_mutable_const/others.rs:79:17
9696
|
97-
LL | let _ = ATOMIC_TUPLE.0[0];
98-
| ^^^^^^^^^^^^
97+
LL | let _ = (&&&&ATOMIC_TUPLE).0;
98+
| ^^^^^^^^^^^^^
99+
|
100+
= help: assign this const to a local or static variable, and use the variable here
101+
102+
error: a `const` item with interior mutability should not be borrowed
103+
--> tests/ui/borrow_interior_mutable_const/others.rs:80:17
104+
|
105+
LL | let _ = (&&&&ATOMIC_TUPLE).2;
106+
| ^^^^^^^^^^^^^
99107
|
100108
= help: assign this const to a local or static variable, and use the variable here
101109

@@ -115,5 +123,5 @@ LL | assert_eq!(CELL.get(), 6);
115123
|
116124
= help: assign this const to a local or static variable, and use the variable here
117125

118-
error: aborting due to 14 previous errors
126+
error: aborting due to 15 previous errors
119127

0 commit comments

Comments
 (0)