Skip to content

Commit a14edd5

Browse files
committed
Auto merge of rust-lang#9246 - kyoto7250:reopen_issue_8493, r=Jarcho
check macro statements in `[non_copy_const]` close rust-lang#8493 close rust-lang#9224 This PR fixes false positives in `[non_copy_const]`. changelog: fix false positives in`[non_copy_const]` --- r? `@Jarcho`
2 parents f847795 + b6c3010 commit a14edd5

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

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: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,25 @@ 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+
thread_local! {
38+
static _BAR: Cell<i32> = const { Cell::new(0) };
39+
}
40+
41+
macro_rules! issue_8493 {
42+
() => {
43+
const _BAZ: Cell<usize> = Cell::new(0); //~ ERROR interior mutable
44+
static _FOOBAR: () = {
45+
thread_local! {
46+
static _VAR: Cell<i32> = const { Cell::new(0) };
47+
}
48+
};
49+
};
50+
}
51+
52+
issue_8493!();
3753
}
3854

3955
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:43: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)