Skip to content

Commit 04849bd

Browse files
committedOct 13, 2024·
Auto merge of #13510 - alex-semenyuk:cleanup_const_float_classify, r=llogiq
Cleanup `const_float_classify` As mentioned at #13508 `const_float_classify` has been stabilized recently in rust-lang/rust#130157 and can be cleanup Close #13508 changelog: [none]
2 parents c71f0be + cb19f23 commit 04849bd

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed
 

‎clippy_config/src/msrvs.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ macro_rules! msrv_aliases {
1717

1818
// names may refer to stabilized feature flags or library items
1919
msrv_aliases! {
20-
1,83,0 { CONST_EXTERN_FN }
21-
1,83,0 { CONST_FLOAT_BITS_CONV }
20+
1,83,0 { CONST_EXTERN_FN, CONST_FLOAT_BITS_CONV, CONST_FLOAT_CLASSIFY }
2221
1,82,0 { IS_NONE_OR }
2322
1,81,0 { LINT_REASONS_STABILIZATION }
2423
1,80,0 { BOX_INTO_ITER}

‎clippy_lints/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
903903
store.register_late_pass(|_| Box::new(manual_range_patterns::ManualRangePatterns));
904904
store.register_early_pass(|| Box::new(visibility::Visibility));
905905
store.register_late_pass(move |_| Box::new(tuple_array_conversions::TupleArrayConversions::new(conf)));
906-
store.register_late_pass(|_| Box::new(manual_float_methods::ManualFloatMethods));
906+
store.register_late_pass(move |_| Box::new(manual_float_methods::ManualFloatMethods::new(conf)));
907907
store.register_late_pass(|_| Box::new(four_forward_slashes::FourForwardSlashes));
908908
store.register_late_pass(|_| Box::new(error_impl_error::ErrorImplError));
909909
store.register_late_pass(move |_| Box::new(absolute_paths::AbsolutePaths::new(conf)));

‎clippy_lints/src/manual_float_methods.rs

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use clippy_config::msrvs::Msrv;
2+
use clippy_config::{Conf, msrvs};
13
use clippy_utils::consts::{ConstEvalCtxt, Constant};
24
use clippy_utils::diagnostics::span_lint_and_then;
35
use clippy_utils::source::SpanRangeExt;
@@ -6,7 +8,7 @@ use rustc_errors::Applicability;
68
use rustc_hir::{BinOpKind, Constness, Expr, ExprKind};
79
use rustc_lint::{LateContext, LateLintPass, Lint, LintContext};
810
use rustc_middle::lint::in_external_macro;
9-
use rustc_session::declare_lint_pass;
11+
use rustc_session::impl_lint_pass;
1012

1113
declare_clippy_lint! {
1214
/// ### What it does
@@ -56,7 +58,7 @@ declare_clippy_lint! {
5658
style,
5759
"use dedicated method to check if a float is finite"
5860
}
59-
declare_lint_pass!(ManualFloatMethods => [MANUAL_IS_INFINITE, MANUAL_IS_FINITE]);
61+
impl_lint_pass!(ManualFloatMethods => [MANUAL_IS_INFINITE, MANUAL_IS_FINITE]);
6062

6163
#[derive(Clone, Copy)]
6264
enum Variant {
@@ -80,6 +82,18 @@ impl Variant {
8082
}
8183
}
8284

85+
pub struct ManualFloatMethods {
86+
msrv: Msrv,
87+
}
88+
89+
impl ManualFloatMethods {
90+
pub fn new(conf: &'static Conf) -> Self {
91+
Self {
92+
msrv: conf.msrv.clone(),
93+
}
94+
}
95+
}
96+
8397
impl<'tcx> LateLintPass<'tcx> for ManualFloatMethods {
8498
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
8599
if let ExprKind::Binary(kind, lhs, rhs) = expr.kind
@@ -92,7 +106,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualFloatMethods {
92106
&& !in_external_macro(cx.sess(), expr.span)
93107
&& (
94108
matches!(cx.tcx.constness(cx.tcx.hir().enclosing_body_owner(expr.hir_id)), Constness::NotConst)
95-
|| cx.tcx.features().declared(sym!(const_float_classify))
109+
|| self.msrv.meets(msrvs::CONST_FLOAT_CLASSIFY)
96110
)
97111
&& let [first, second, const_1, const_2] = exprs
98112
&& let ecx = ConstEvalCtxt::new(cx)
@@ -150,6 +164,8 @@ impl<'tcx> LateLintPass<'tcx> for ManualFloatMethods {
150164
});
151165
}
152166
}
167+
168+
extract_msrv_attr!(LateContext);
153169
}
154170

155171
fn is_infinity(constant: &Constant<'_>) -> bool {

‎tests/ui/manual_float_methods.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ fn main() {
3939
if x != f64::INFINITY && x != fn_test() {}
4040
// Not -inf
4141
if x != f64::INFINITY && x != fn_test_not_inf() {}
42+
const {
43+
let x = 1.0f64;
44+
if x == f64::INFINITY || x == f64::NEG_INFINITY {}
45+
}
4246
const X: f64 = 1.0f64;
43-
// Will be linted if `const_float_classify` is enabled
4447
if const { X == f64::INFINITY || X == f64::NEG_INFINITY } {}
4548
if const { X != f64::INFINITY && X != f64::NEG_INFINITY } {}
4649
external! {

‎tests/ui/manual_float_methods.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,11 @@ help: or, for conciseness
7878
LL | if !x.is_infinite() {}
7979
| ~~~~~~~~~~~~~~~~
8080

81-
error: aborting due to 6 previous errors
81+
error: manually checking if a float is infinite
82+
--> tests/ui/manual_float_methods.rs:44:12
83+
|
84+
LL | if x == f64::INFINITY || x == f64::NEG_INFINITY {}
85+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the dedicated method instead: `x.is_infinite()`
86+
87+
error: aborting due to 7 previous errors
8288

0 commit comments

Comments
 (0)