use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::in_constant;
+use clippy_utils::macros::macro_backtrace;
use if_chain::if_chain;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::DefId;
BodyId, Expr, ExprKind, HirId, Impl, ImplItem, ImplItemKind, Item, ItemKind, Node, TraitItem, TraitItemKind, UnOp,
};
use rustc_lint::{LateContext, LateLintPass, Lint};
+use rustc_middle::mir;
use rustc_middle::mir::interpret::{ConstValue, ErrorHandled};
use rustc_middle::ty::adjustment::Adjust;
-use rustc_middle::ty::{self, Const, Ty};
+use rustc_middle::ty::{self, Ty};
use rustc_session::{declare_lint_pass, declare_tool_lint};
-use rustc_span::{InnerSpan, Span, DUMMY_SP};
+use rustc_span::{sym, InnerSpan, Span, DUMMY_SP};
use rustc_typeck::hir_ty_to_ty;
// FIXME: this is a correctness problem but there's no suitable
result: Result<ConstValue<'tcx>, ErrorHandled>,
ty: Ty<'tcx>,
) -> bool {
- fn inner<'tcx>(cx: &LateContext<'tcx>, val: Const<'tcx>) -> bool {
+ fn inner<'tcx>(cx: &LateContext<'tcx>, val: mir::ConstantKind<'tcx>) -> bool {
match val.ty().kind() {
// the fact that we have to dig into every structs to search enums
// leads us to the point checking `UnsafeCell` directly is the only option.
- ty::Adt(ty_def, ..) if Some(ty_def.did()) == cx.tcx.lang_items().unsafe_cell_type() => true,
+ ty::Adt(ty_def, ..) if ty_def.is_unsafe_cell() => true,
ty::Array(..) | ty::Adt(..) | ty::Tuple(..) => {
- let val = cx.tcx.destructure_const(cx.param_env.and(val));
+ let val = cx.tcx.destructure_mir_constant(cx.param_env, val);
val.fields.iter().any(|field| inner(cx, *field))
},
_ => false,
}
}
-
result.map_or_else(
|err| {
// Consider `TooGeneric` cases as being unfrozen.
// I chose this way because unfrozen enums as assoc consts are rare (or, hopefully, none).
err == ErrorHandled::TooGeneric
},
- |val| inner(cx, Const::from_value(cx.tcx, val, ty)),
+ |val| inner(cx, mir::ConstantKind::from_value(val, ty)),
)
}
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 is_unfrozen(cx, ty) && is_value_unfrozen_poly(cx, body_id, 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)
+ {
lint(cx, Source::Item { item: it.span });
}
}