Skip to content

Commit

Permalink
Auto merge of rust-lang#9246 - kyoto7250:reopen_issue_8493, r=Jarcho
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
bors committed Jul 25, 2022
2 parents f847795 + b6c3010 commit a14edd5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
18 changes: 10 additions & 8 deletions clippy_lints/src/non_copy_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,7 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst {
fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx Item<'_>) {
if let ItemKind::Const(hir_ty, body_id) = it.kind {
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
if !macro_backtrace(it.span).last().map_or(false, |macro_call| {
matches!(
cx.tcx.get_diagnostic_name(macro_call.def_id),
Some(sym::thread_local_macro)
)
}) && is_unfrozen(cx, ty)
&& is_value_unfrozen_poly(cx, body_id, ty)
{
if !ignored_macro(cx, it) && is_unfrozen(cx, ty) && is_value_unfrozen_poly(cx, body_id, ty) {
lint(cx, Source::Item { item: it.span });
}
}
Expand Down Expand Up @@ -445,3 +438,12 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst {
}
}
}

fn ignored_macro(cx: &LateContext<'_>, it: &rustc_hir::Item<'_>) -> bool {
macro_backtrace(it.span).any(|macro_call| {
matches!(
cx.tcx.get_diagnostic_name(macro_call.def_id),
Some(sym::thread_local_macro)
)
})
}
22 changes: 19 additions & 3 deletions tests/ui/declare_interior_mutable_const/others.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,25 @@ const NO_ANN: &dyn Display = &70;
static STATIC_TUPLE: (AtomicUsize, String) = (ATOMIC, STRING);
//^ there should be no lints on this line

// issue #8493
thread_local! {
static THREAD_LOCAL: Cell<i32> = const { Cell::new(0) };
mod issue_8493 {
use std::cell::Cell;

thread_local! {
static _BAR: Cell<i32> = const { Cell::new(0) };
}

macro_rules! issue_8493 {
() => {
const _BAZ: Cell<usize> = Cell::new(0); //~ ERROR interior mutable
static _FOOBAR: () = {
thread_local! {
static _VAR: Cell<i32> = const { Cell::new(0) };
}
};
};
}

issue_8493!();
}

fn main() {}
13 changes: 12 additions & 1 deletion tests/ui/declare_interior_mutable_const/others.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,16 @@ LL | declare_const!(_ONCE: Once = Once::new()); //~ ERROR interior mutable
|
= note: this error originates in the macro `declare_const` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 4 previous errors
error: a `const` item should never be interior mutable
--> $DIR/others.rs:43:13
|
LL | const _BAZ: Cell<usize> = Cell::new(0); //~ ERROR interior mutable
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
LL | issue_8493!();
| ------------- in this macro invocation
|
= 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

0 comments on commit a14edd5

Please sign in to comment.