Skip to content

Commit

Permalink
Auto merge of #13510 - alex-semenyuk:cleanup_const_float_classify, r=…
Browse files Browse the repository at this point in the history
…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]
  • Loading branch information
bors committed Oct 13, 2024
2 parents c71f0be + cb19f23 commit 04849bd
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
3 changes: 1 addition & 2 deletions clippy_config/src/msrvs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ macro_rules! msrv_aliases {

// names may refer to stabilized feature flags or library items
msrv_aliases! {
1,83,0 { CONST_EXTERN_FN }
1,83,0 { CONST_FLOAT_BITS_CONV }
1,83,0 { CONST_EXTERN_FN, CONST_FLOAT_BITS_CONV, CONST_FLOAT_CLASSIFY }
1,82,0 { IS_NONE_OR }
1,81,0 { LINT_REASONS_STABILIZATION }
1,80,0 { BOX_INTO_ITER}
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
store.register_late_pass(|_| Box::new(manual_range_patterns::ManualRangePatterns));
store.register_early_pass(|| Box::new(visibility::Visibility));
store.register_late_pass(move |_| Box::new(tuple_array_conversions::TupleArrayConversions::new(conf)));
store.register_late_pass(|_| Box::new(manual_float_methods::ManualFloatMethods));
store.register_late_pass(move |_| Box::new(manual_float_methods::ManualFloatMethods::new(conf)));
store.register_late_pass(|_| Box::new(four_forward_slashes::FourForwardSlashes));
store.register_late_pass(|_| Box::new(error_impl_error::ErrorImplError));
store.register_late_pass(move |_| Box::new(absolute_paths::AbsolutePaths::new(conf)));
Expand Down
22 changes: 19 additions & 3 deletions clippy_lints/src/manual_float_methods.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use clippy_config::msrvs::Msrv;
use clippy_config::{Conf, msrvs};
use clippy_utils::consts::{ConstEvalCtxt, Constant};
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::SpanRangeExt;
Expand All @@ -6,7 +8,7 @@ use rustc_errors::Applicability;
use rustc_hir::{BinOpKind, Constness, Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass, Lint, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_session::declare_lint_pass;
use rustc_session::impl_lint_pass;

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -56,7 +58,7 @@ declare_clippy_lint! {
style,
"use dedicated method to check if a float is finite"
}
declare_lint_pass!(ManualFloatMethods => [MANUAL_IS_INFINITE, MANUAL_IS_FINITE]);
impl_lint_pass!(ManualFloatMethods => [MANUAL_IS_INFINITE, MANUAL_IS_FINITE]);

#[derive(Clone, Copy)]
enum Variant {
Expand All @@ -80,6 +82,18 @@ impl Variant {
}
}

pub struct ManualFloatMethods {
msrv: Msrv,
}

impl ManualFloatMethods {
pub fn new(conf: &'static Conf) -> Self {
Self {
msrv: conf.msrv.clone(),
}
}
}

impl<'tcx> LateLintPass<'tcx> for ManualFloatMethods {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
if let ExprKind::Binary(kind, lhs, rhs) = expr.kind
Expand All @@ -92,7 +106,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualFloatMethods {
&& !in_external_macro(cx.sess(), expr.span)
&& (
matches!(cx.tcx.constness(cx.tcx.hir().enclosing_body_owner(expr.hir_id)), Constness::NotConst)
|| cx.tcx.features().declared(sym!(const_float_classify))
|| self.msrv.meets(msrvs::CONST_FLOAT_CLASSIFY)
)
&& let [first, second, const_1, const_2] = exprs
&& let ecx = ConstEvalCtxt::new(cx)
Expand Down Expand Up @@ -150,6 +164,8 @@ impl<'tcx> LateLintPass<'tcx> for ManualFloatMethods {
});
}
}

extract_msrv_attr!(LateContext);
}

fn is_infinity(constant: &Constant<'_>) -> bool {
Expand Down
5 changes: 4 additions & 1 deletion tests/ui/manual_float_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ fn main() {
if x != f64::INFINITY && x != fn_test() {}
// Not -inf
if x != f64::INFINITY && x != fn_test_not_inf() {}
const {
let x = 1.0f64;
if x == f64::INFINITY || x == f64::NEG_INFINITY {}
}
const X: f64 = 1.0f64;
// Will be linted if `const_float_classify` is enabled
if const { X == f64::INFINITY || X == f64::NEG_INFINITY } {}
if const { X != f64::INFINITY && X != f64::NEG_INFINITY } {}
external! {
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/manual_float_methods.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,11 @@ help: or, for conciseness
LL | if !x.is_infinite() {}
| ~~~~~~~~~~~~~~~~

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

error: aborting due to 7 previous errors

0 comments on commit 04849bd

Please sign in to comment.