Skip to content

Commit f865ada

Browse files
committed
Require the constness query to only be invoked on things that can have constness
1 parent 1857833 commit f865ada

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)