Skip to content

Commit 6e9a7f4

Browse files
committed
nerf the clippy lint, the rustc APIs just don't work the way it wants them to
1 parent 4f5055d commit 6e9a7f4

File tree

9 files changed

+71
-193
lines changed

9 files changed

+71
-193
lines changed

src/tools/clippy/clippy_lints/src/non_copy_const.rs

+19-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_hir::{
1212
BodyId, Expr, ExprKind, HirId, Impl, ImplItem, ImplItemKind, Item, ItemKind, Node, TraitItem, TraitItemKind, UnOp,
1313
};
1414
use rustc_lint::{LateContext, LateLintPass, Lint};
15-
use rustc_middle::mir::interpret::ErrorHandled;
15+
use rustc_middle::mir::interpret::{ErrorHandled, GlobalId};
1616
use rustc_middle::ty::adjustment::Adjust;
1717
use rustc_middle::ty::{self, Ty, TyCtxt};
1818
use rustc_session::impl_lint_pass;
@@ -232,7 +232,7 @@ impl<'tcx> NonCopyConst<'tcx> {
232232
ty: Ty<'tcx>,
233233
) -> bool {
234234
result.map_or_else(
235-
|err| {
235+
|_err| {
236236
// Consider `TooGeneric` cases as being unfrozen.
237237
// This causes a false positive where an assoc const whose type is unfrozen
238238
// have a value that is a frozen variant with a generic param (an example is
@@ -254,19 +254,30 @@ impl<'tcx> NonCopyConst<'tcx> {
254254
// similar to 2., but with a frozen variant) (e.g. borrowing
255255
// `borrow_interior_mutable_const::enums::AssocConsts::TO_BE_FROZEN_VARIANT`).
256256
// I chose this way because unfrozen enums as assoc consts are rare (or, hopefully, none).
257-
matches!(err, ErrorHandled::TooGeneric(..))
257+
258+
// Update (2025-05-12): After some compiler refactoring, the number of false positives
259+
// caused by this has drastically increased, so we no longer warn against generic consts.
260+
false
261+
},
262+
|val| {
263+
// FIXME: The `true` here leads to false positives for any constant with a
264+
// non-valtree-compatible type.
265+
val.map_or(true, |val| Self::is_value_unfrozen_raw_inner(cx, val, ty))
258266
},
259-
|val| val.map_or(true, |val| Self::is_value_unfrozen_raw_inner(cx, val, ty)),
260267
)
261268
}
262269

263270
fn is_value_unfrozen_poly(cx: &LateContext<'tcx>, body_id: BodyId, ty: Ty<'tcx>) -> bool {
264271
let def_id = body_id.hir_id.owner.to_def_id();
265-
let typing_env = ty::TypingEnv::post_analysis(cx.tcx, def_id);
266-
267272
let args = ty::GenericArgs::identity_for_item(cx.tcx, def_id);
268-
let const_ = ty::UnevaluatedConst::new(def_id, args);
269-
let result = cx.tcx.const_eval_resolve_for_typeck(typing_env, const_, DUMMY_SP);
273+
let instance = ty::Instance::new_raw(def_id, args);
274+
let cid = GlobalId {
275+
instance,
276+
promoted: None,
277+
};
278+
let typing_env = ty::TypingEnv::post_analysis(cx.tcx, def_id);
279+
// We do *not* want to "resolve" this `cid`, that seems to help the lint evaluate more contants.
280+
let result = cx.tcx.const_eval_global_id_for_typeck(typing_env, cid, DUMMY_SP);
270281
Self::is_value_unfrozen_raw(cx, result, ty)
271282
}
272283

src/tools/clippy/tests/ui/borrow_interior_mutable_const/enums.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
#![deny(clippy::borrow_interior_mutable_const)]
44
#![allow(clippy::declare_interior_mutable_const)]
55

6-
// this file (mostly) replicates its `declare` counterpart. Please see it for more discussions.
6+
// this file (mostly) replicates its counterpart in `declare_interior_mutable_const`. Please see it
7+
// for more discussions.
8+
9+
// FIXME: there are hardly any warnings since the lint no longer works on generic consts.
710

811
extern crate helper;
912

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

3942
// The lint ignores default values because an impl of this trait can set
4043
// 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
44+
let _ = &Self::DEFAULTED_ON_FROZEN_VARIANT;
4245
}
4346
}
4447

@@ -88,8 +91,8 @@ impl<T> BothOfCellAndGeneric<T> {
8891
const FROZEN_VARIANT: BothOfCellAndGeneric<T> = BothOfCellAndGeneric::Frozen(5);
8992

9093
fn function() {
91-
let _ = &Self::UNFROZEN_VARIANT; //~ ERROR: interior mutability
92-
let _ = &Self::GENERIC_VARIANT; //~ ERROR: interior mutability
94+
let _ = &Self::UNFROZEN_VARIANT;
95+
let _ = &Self::GENERIC_VARIANT;
9396
let _ = &Self::FROZEN_VARIANT;
9497
}
9598
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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:25:14
33
|
44
LL | let _ = &UNFROZEN_VARIANT;
55
| ^^^^^^^^^^^^^^^^
@@ -12,68 +12,36 @@ 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:53:18
3216
|
3317
LL | let _ = &<Self as AssocConsts>::TO_BE_UNFROZEN_VARIANT;
3418
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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:55:18
4024
|
4125
LL | let _ = &Self::DEFAULTED_ON_UNFROZEN_VARIANT;
4226
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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:77:18
4832
|
4933
LL | let _ = &<Self as AssocTypes>::TO_BE_UNFROZEN_VARIANT;
5034
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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
56-
|
57-
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-
| ^^^^^^^^^^^^^^^^^^^^^
67-
|
68-
= help: assign this const to a local or static variable, and use the variable here
69-
70-
error: a `const` item with interior mutability should not be borrowed
71-
--> tests/ui/borrow_interior_mutable_const/enums.rs:99:14
39+
--> tests/ui/borrow_interior_mutable_const/enums.rs:102:14
7240
|
7341
LL | let _ = &helper::WRAPPED_PRIVATE_UNFROZEN_VARIANT;
7442
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7543
|
7644
= help: assign this const to a local or static variable, and use the variable here
7745

78-
error: aborting due to 9 previous errors
46+
error: aborting due to 5 previous errors
7947

src/tools/clippy/tests/ui/borrow_interior_mutable_const/traits.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#![deny(clippy::borrow_interior_mutable_const)]
22
#![allow(clippy::declare_interior_mutable_const)]
33

4-
// this file replicates its `declare` counterpart. Please see it for more discussions.
4+
// this file replicates its counterpart in `declare_interior_mutable_const`. Please see it for more
5+
// discussions.
6+
7+
// FIXME: there are hardly any warnings since the lint no longer works on generic consts.
58

69
use std::borrow::Cow;
710
use std::cell::Cell;
@@ -13,7 +16,6 @@ trait ConcreteTypes {
1316

1417
fn function() {
1518
let _ = &Self::ATOMIC;
16-
//~^ borrow_interior_mutable_const
1719
let _ = &Self::STRING;
1820
}
1921
}
@@ -112,7 +114,6 @@ where
112114
fn function() {
113115
let _ = &Self::NOT_BOUNDED;
114116
let _ = &Self::BOUNDED;
115-
//~^ borrow_interior_mutable_const
116117
}
117118
}
118119

@@ -168,9 +169,7 @@ trait BothOfCellAndGeneric<T> {
168169

169170
fn function() {
170171
let _ = &Self::DIRECT;
171-
//~^ borrow_interior_mutable_const
172172
let _ = &Self::INDIRECT;
173-
//~^ borrow_interior_mutable_const
174173
}
175174
}
176175

@@ -180,9 +179,7 @@ impl<T: ConstDefault> BothOfCellAndGeneric<T> for Vec<T> {
180179

181180
fn function() {
182181
let _ = &Self::DIRECT;
183-
//~^ borrow_interior_mutable_const
184182
let _ = &Self::INDIRECT;
185-
//~^ borrow_interior_mutable_const
186183
}
187184
}
188185

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: a `const` item with interior mutability should not be borrowed
2-
--> tests/ui/borrow_interior_mutable_const/traits.rs:15:18
2+
--> tests/ui/borrow_interior_mutable_const/traits.rs:29:18
33
|
44
LL | let _ = &Self::ATOMIC;
55
| ^^^^^^^^^^^^
@@ -12,132 +12,84 @@ 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/traits.rs:27:18
16-
|
17-
LL | let _ = &Self::ATOMIC;
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/traits.rs:53:18
15+
--> tests/ui/borrow_interior_mutable_const/traits.rs:55:18
2416
|
2517
LL | let _ = &Self::TO_BE_CONCRETE;
2618
| ^^^^^^^^^^^^^^^^^^^^
2719
|
2820
= help: assign this const to a local or static variable, and use the variable here
2921

3022
error: a `const` item with interior mutability should not be borrowed
31-
--> tests/ui/borrow_interior_mutable_const/traits.rs:89:18
23+
--> tests/ui/borrow_interior_mutable_const/traits.rs:91:18
3224
|
3325
LL | let _ = &Self::TO_BE_UNFROZEN;
3426
| ^^^^^^^^^^^^^^^^^^^^
3527
|
3628
= help: assign this const to a local or static variable, and use the variable here
3729

3830
error: a `const` item with interior mutability should not be borrowed
39-
--> tests/ui/borrow_interior_mutable_const/traits.rs:91:18
31+
--> tests/ui/borrow_interior_mutable_const/traits.rs:93:18
4032
|
4133
LL | let _ = &Self::WRAPPED_TO_BE_UNFROZEN;
4234
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4335
|
4436
= help: assign this const to a local or static variable, and use the variable here
4537

4638
error: a `const` item with interior mutability should not be borrowed
47-
--> tests/ui/borrow_interior_mutable_const/traits.rs:114:18
39+
--> tests/ui/borrow_interior_mutable_const/traits.rs:129:18
4840
|
4941
LL | let _ = &Self::BOUNDED;
5042
| ^^^^^^^^^^^^^
5143
|
5244
= help: assign this const to a local or static variable, and use the variable here
5345

5446
error: a `const` item with interior mutability should not be borrowed
55-
--> tests/ui/borrow_interior_mutable_const/traits.rs:128:18
56-
|
57-
LL | let _ = &Self::BOUNDED;
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/traits.rs:158:18
47+
--> tests/ui/borrow_interior_mutable_const/traits.rs:159:18
6448
|
6549
LL | let _ = &Self::SELF;
6650
| ^^^^^^^^^^
6751
|
6852
= help: assign this const to a local or static variable, and use the variable here
6953

7054
error: a `const` item with interior mutability should not be borrowed
71-
--> tests/ui/borrow_interior_mutable_const/traits.rs:160:18
55+
--> tests/ui/borrow_interior_mutable_const/traits.rs:161:18
7256
|
7357
LL | let _ = &Self::WRAPPED_SELF;
7458
| ^^^^^^^^^^^^^^^^^^
7559
|
7660
= help: assign this const to a local or static variable, and use the variable here
7761

7862
error: a `const` item with interior mutability should not be borrowed
79-
--> tests/ui/borrow_interior_mutable_const/traits.rs:170:18
80-
|
81-
LL | let _ = &Self::DIRECT;
82-
| ^^^^^^^^^^^^
83-
|
84-
= help: assign this const to a local or static variable, and use the variable here
85-
86-
error: a `const` item with interior mutability should not be borrowed
87-
--> tests/ui/borrow_interior_mutable_const/traits.rs:172:18
88-
|
89-
LL | let _ = &Self::INDIRECT;
90-
| ^^^^^^^^^^^^^^
91-
|
92-
= help: assign this const to a local or static variable, and use the variable here
93-
94-
error: a `const` item with interior mutability should not be borrowed
95-
--> tests/ui/borrow_interior_mutable_const/traits.rs:182:18
96-
|
97-
LL | let _ = &Self::DIRECT;
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/traits.rs:184:18
104-
|
105-
LL | let _ = &Self::INDIRECT;
106-
| ^^^^^^^^^^^^^^
107-
|
108-
= help: assign this const to a local or static variable, and use the variable here
109-
110-
error: a `const` item with interior mutability should not be borrowed
111-
--> tests/ui/borrow_interior_mutable_const/traits.rs:204:18
63+
--> tests/ui/borrow_interior_mutable_const/traits.rs:201:18
11264
|
11365
LL | let _ = &Self::ATOMIC;
11466
| ^^^^^^^^^^^^
11567
|
11668
= help: assign this const to a local or static variable, and use the variable here
11769

11870
error: a `const` item with interior mutability should not be borrowed
119-
--> tests/ui/borrow_interior_mutable_const/traits.rs:209:18
71+
--> tests/ui/borrow_interior_mutable_const/traits.rs:206:18
12072
|
12173
LL | let _ = &Self::BOUNDED_ASSOC_TYPE;
12274
| ^^^^^^^^^^^^^^^^^^^^^^^^
12375
|
12476
= help: assign this const to a local or static variable, and use the variable here
12577

12678
error: a `const` item with interior mutability should not be borrowed
127-
--> tests/ui/borrow_interior_mutable_const/traits.rs:215:5
79+
--> tests/ui/borrow_interior_mutable_const/traits.rs:212:5
12880
|
12981
LL | u64::ATOMIC.store(5, Ordering::SeqCst);
13082
| ^^^^^^^^^^^
13183
|
13284
= help: assign this const to a local or static variable, and use the variable here
13385

13486
error: a `const` item with interior mutability should not be borrowed
135-
--> tests/ui/borrow_interior_mutable_const/traits.rs:217:16
87+
--> tests/ui/borrow_interior_mutable_const/traits.rs:214:16
13688
|
13789
LL | assert_eq!(u64::ATOMIC.load(Ordering::SeqCst), 9);
13890
| ^^^^^^^^^^^
13991
|
14092
= help: assign this const to a local or static variable, and use the variable here
14193

142-
error: aborting due to 17 previous errors
94+
error: aborting due to 11 previous errors
14395

0 commit comments

Comments
 (0)