Skip to content

Commit 4773564

Browse files
committed
check macro statements in non_copy_const.rs
1 parent 4a5b7e2 commit 4773564

File tree

4 files changed

+47
-13
lines changed

4 files changed

+47
-13
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ serde = { version = "1.0.125", features = ["derive"] }
5252
syn = { version = "1.0", features = ["full"] }
5353
futures = "0.3"
5454
parking_lot = "0.12"
55-
tokio = { version = "1", features = ["io-util"] }
55+
tokio = { version = "1", features = ["full"] }
5656
rustc-semver = "1.1"
5757

5858
[build-dependencies]

clippy_lints/src/non_copy_const.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,7 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst {
251251
fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx Item<'_>) {
252252
if let ItemKind::Const(hir_ty, body_id) = it.kind {
253253
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
254-
if !macro_backtrace(it.span).last().map_or(false, |macro_call| {
255-
matches!(
256-
cx.tcx.get_diagnostic_name(macro_call.def_id),
257-
Some(sym::thread_local_macro)
258-
)
259-
}) && is_unfrozen(cx, ty)
260-
&& is_value_unfrozen_poly(cx, body_id, ty)
261-
{
254+
if !ignored_macro(cx, it) && is_unfrozen(cx, ty) && is_value_unfrozen_poly(cx, body_id, ty) {
262255
lint(cx, Source::Item { item: it.span });
263256
}
264257
}
@@ -445,3 +438,12 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst {
445438
}
446439
}
447440
}
441+
442+
fn ignored_macro(cx: &LateContext<'_>, it: &rustc_hir::Item<'_>) -> bool {
443+
macro_backtrace(it.span).any(|macro_call| {
444+
matches!(
445+
cx.tcx.get_diagnostic_name(macro_call.def_id),
446+
Some(sym::thread_local_macro)
447+
)
448+
})
449+
}

tests/ui/declare_interior_mutable_const/others.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,30 @@ const NO_ANN: &dyn Display = &70;
3131
static STATIC_TUPLE: (AtomicUsize, String) = (ATOMIC, STRING);
3232
//^ there should be no lints on this line
3333

34-
// issue #8493
35-
thread_local! {
36-
static THREAD_LOCAL: Cell<i32> = const { Cell::new(0) };
34+
mod issue_8493 {
35+
use std::cell::Cell;
36+
37+
// https://github.com/rust-lang/rust-clippy/issues/9224
38+
tokio::task_local! {
39+
pub static _FOO: String;
40+
}
41+
42+
thread_local! {
43+
static _BAR: Cell<i32> = const { Cell::new(0) };
44+
}
45+
46+
macro_rules! issue_8493 {
47+
() => {
48+
const _BAZ: Cell<usize> = Cell::new(0); //~ ERROR interior mutable
49+
static _FOOBAR: () = {
50+
thread_local! {
51+
static _VAR: Cell<i32> = const { Cell::new(0) };
52+
}
53+
};
54+
};
55+
}
56+
57+
issue_8493!();
3758
}
3859

3960
fn main() {}

tests/ui/declare_interior_mutable_const/others.stderr

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,16 @@ LL | declare_const!(_ONCE: Once = Once::new()); //~ ERROR interior mutable
3535
|
3636
= note: this error originates in the macro `declare_const` (in Nightly builds, run with -Z macro-backtrace for more info)
3737

38-
error: aborting due to 4 previous errors
38+
error: a `const` item should never be interior mutable
39+
--> $DIR/others.rs:48:13
40+
|
41+
LL | const _BAZ: Cell<usize> = Cell::new(0); //~ ERROR interior mutable
42+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
43+
...
44+
LL | issue_8493!();
45+
| ------------- in this macro invocation
46+
|
47+
= note: this error originates in the macro `issue_8493` (in Nightly builds, run with -Z macro-backtrace for more info)
48+
49+
error: aborting due to 5 previous errors
3950

0 commit comments

Comments
 (0)