Clean up const_float_classify leftovers

This commit is contained in:
Alexey Semenyuk 2024-10-06 16:32:49 +05:00
parent 1f8f982f22
commit cb19f23c79
5 changed files with 32 additions and 8 deletions

View File

@ -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}

View File

@ -899,7 +899,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)));

View File

@ -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;
@ -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
@ -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 {
@ -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
@ -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)
@ -150,6 +164,8 @@ impl<'tcx> LateLintPass<'tcx> for ManualFloatMethods {
});
}
}
extract_msrv_attr!(LateContext);
}
fn is_infinity(constant: &Constant<'_>) -> bool {

View File

@ -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! {

View File

@ -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