Skip to content

Commit 8a09650

Browse files
committed
Auto merge of rust-lang#134122 - oli-obk:push-zqnyznxtpnll, r=petrochenkov
Move impl constness into impl trait header This PR is kind of the opposite of the rejected rust-lang#134114 Instead of moving more things into the `constness` query, we want to keep them where their corresponding hir nodes are lowered. So I gave this a spin for impls, which have an obvious place to be (the impl trait header). And surprisingly it's also a perf improvement (likely just slightly better query & cache usage). The issue was that removing anything from the `constness` query makes it just return `NotConst`, which is wrong. So I had to change it to `bug!` out if used wrongly, and only then remove the impl blocks from the `constness` query. I think this change is good in general, because it makes using `constness` more robust (as can be seen by how few sites that had to be changed, so it was almost solely used specifically for the purpose of asking for functions' constness). The main thing where this change was not great was in clippy, which was using the `constness` query as a general DefId -> constness map. I added a `DefKind` filter in front of that. If it becomes a more common pattern we can always move that helper into rustc.
2 parents 51ff984 + f865ada commit 8a09650

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

clippy_lints/src/manual_float_methods.rs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ use clippy_utils::source::SpanRangeExt;
66
use clippy_utils::{is_from_proc_macro, path_to_local};
77
use rustc_errors::Applicability;
88
use rustc_hir::{BinOpKind, Constness, Expr, ExprKind};
9+
use rustc_hir::def::DefKind;
10+
use rustc_hir::def_id::DefId;
911
use rustc_lint::{LateContext, LateLintPass, Lint, LintContext};
1012
use rustc_middle::lint::in_external_macro;
13+
use rustc_middle::ty::TyCtxt;
1114
use rustc_session::impl_lint_pass;
1215

1316
declare_clippy_lint! {
@@ -94,6 +97,44 @@ impl ManualFloatMethods {
9497
}
9598
}
9699

100+
fn is_not_const(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
101+
match tcx.def_kind(def_id) {
102+
DefKind::Mod
103+
| DefKind::Struct
104+
| DefKind::Union
105+
| DefKind::Enum
106+
| DefKind::Variant
107+
| DefKind::Trait
108+
| DefKind::TyAlias
109+
| DefKind::ForeignTy
110+
| DefKind::TraitAlias
111+
| DefKind::AssocTy
112+
| DefKind::Macro(..)
113+
| DefKind::Field
114+
| DefKind::LifetimeParam
115+
| DefKind::ExternCrate
116+
| DefKind::Use
117+
| DefKind::ForeignMod
118+
| DefKind::GlobalAsm
119+
| DefKind::Impl { .. }
120+
| DefKind::OpaqueTy
121+
| DefKind::SyntheticCoroutineBody
122+
| DefKind::TyParam => true,
123+
124+
DefKind::AnonConst
125+
| DefKind::InlineConst
126+
| DefKind::Const
127+
| DefKind::ConstParam
128+
| DefKind::Static { .. }
129+
| DefKind::Ctor(..)
130+
| DefKind::AssocConst => false,
131+
132+
DefKind::Fn
133+
| DefKind::AssocFn
134+
| DefKind::Closure => tcx.constness(def_id) == Constness::NotConst,
135+
}
136+
}
137+
97138
impl<'tcx> LateLintPass<'tcx> for ManualFloatMethods {
98139
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
99140
if let ExprKind::Binary(kind, lhs, rhs) = expr.kind
@@ -105,7 +146,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualFloatMethods {
105146
&& exprs.iter_mut().partition_in_place(|i| path_to_local(i).is_some()) == 2
106147
&& !in_external_macro(cx.sess(), expr.span)
107148
&& (
108-
matches!(cx.tcx.constness(cx.tcx.hir().enclosing_body_owner(expr.hir_id)), Constness::NotConst)
149+
is_not_const(cx.tcx, cx.tcx.hir().enclosing_body_owner(expr.hir_id).into())
109150
|| self.msrv.meets(msrvs::CONST_FLOAT_CLASSIFY)
110151
)
111152
&& let [first, second, const_1, const_2] = exprs

0 commit comments

Comments
 (0)