@@ -6,8 +6,11 @@ use clippy_utils::source::SpanRangeExt;
6
6
use clippy_utils:: { is_from_proc_macro, path_to_local} ;
7
7
use rustc_errors:: Applicability ;
8
8
use rustc_hir:: { BinOpKind , Constness , Expr , ExprKind } ;
9
+ use rustc_hir:: def:: DefKind ;
10
+ use rustc_hir:: def_id:: DefId ;
9
11
use rustc_lint:: { LateContext , LateLintPass , Lint , LintContext } ;
10
12
use rustc_middle:: lint:: in_external_macro;
13
+ use rustc_middle:: ty:: TyCtxt ;
11
14
use rustc_session:: impl_lint_pass;
12
15
13
16
declare_clippy_lint ! {
@@ -94,6 +97,44 @@ impl ManualFloatMethods {
94
97
}
95
98
}
96
99
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
+
97
138
impl < ' tcx > LateLintPass < ' tcx > for ManualFloatMethods {
98
139
fn check_expr ( & mut self , cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' tcx > ) {
99
140
if let ExprKind :: Binary ( kind, lhs, rhs) = expr. kind
@@ -105,7 +146,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualFloatMethods {
105
146
&& exprs. iter_mut ( ) . partition_in_place ( |i| path_to_local ( i) . is_some ( ) ) == 2
106
147
&& !in_external_macro ( cx. sess ( ) , expr. span )
107
148
&& (
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 ( ) )
109
150
|| self . msrv . meets ( msrvs:: CONST_FLOAT_CLASSIFY )
110
151
)
111
152
&& let [ first, second, const_1, const_2] = exprs
0 commit comments