Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

float_cmp changes #11948

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Deprecate float_cmp_const
  • Loading branch information
Jarcho committed Jul 18, 2024
commit 44e3fdc19423360c4569134d2b43b98d69fc96d3
1 change: 0 additions & 1 deletion clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,6 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
crate::operators::ERASING_OP_INFO,
crate::operators::FLOAT_ARITHMETIC_INFO,
crate::operators::FLOAT_CMP_INFO,
crate::operators::FLOAT_CMP_CONST_INFO,
crate::operators::FLOAT_EQUALITY_WITHOUT_ABS_INFO,
crate::operators::IDENTITY_OP_INFO,
crate::operators::IMPOSSIBLE_COMPARISONS_INFO,
Expand Down
11 changes: 11 additions & 0 deletions clippy_lints/src/deprecated_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,14 @@ declare_deprecated_lint! {
pub MISMATCHED_TARGET_OS,
"this lint has been replaced by `unexpected_cfgs`"
}

declare_deprecated_lint! {
/// ### What it does
/// Nothing. This lint has been deprecated.
///
/// ### Deprecation reason
/// `float_cmp` handles this via config options
#[clippy::version = "1.76.0"]
pub FLOAT_CMP_CONST,
"`float_cmp` handles this via config options"
}
4 changes: 4 additions & 0 deletions clippy_lints/src/lib.deprecated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,8 @@
"clippy::mismatched_target_os",
"this lint has been replaced by `unexpected_cfgs`",
);
store.register_removed(
"clippy::float_cmp_const",
"`float_cmp` handles this via config options",
);
}
66 changes: 1 addition & 65 deletions clippy_lints/src/operators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,70 +633,7 @@ declare_clippy_lint! {

declare_clippy_lint! {
/// ### What it does
/// Checks for (in-)equality comparisons on constant floating-point
/// values (apart from zero), except in functions called `*eq*` (which probably
/// implement equality for a type involving floats).
///
/// ### Why restrict this?
/// Floating point calculations are usually imprecise, so asking if two values are *exactly*
/// equal is asking for trouble because arriving at the same logical result via different
/// routes (e.g. calculation versus constant) may yield different values.
///
/// ### Example
///
/// ```no_run
/// let a: f64 = 1000.1;
/// let b: f64 = 0.2;
/// let x = a + b;
/// const Y: f64 = 1000.3; // Expected value.
///
/// // Actual value: 1000.3000000000001
/// println!("{x}");
///
/// let are_equal = x == Y;
/// println!("{are_equal}"); // false
/// ```
///
/// The correct way to compare floating point numbers is to define an allowed error margin. This
/// may be challenging if there is no "natural" error margin to permit. Broadly speaking, there
/// are two cases:
///
/// 1. If your values are in a known range and you can define a threshold for "close enough to
/// be equal", it may be appropriate to define an absolute error margin. For example, if your
/// data is "length of vehicle in centimeters", you may consider 0.1 cm to be "close enough".
/// 1. If your code is more general and you do not know the range of values, you should use a
/// relative error margin, accepting e.g. 0.1% of error regardless of specific values.
///
/// For the scenario where you can define a meaningful absolute error margin, consider using:
///
/// ```no_run
/// let a: f64 = 1000.1;
/// let b: f64 = 0.2;
/// let x = a + b;
/// const Y: f64 = 1000.3; // Expected value.
///
/// const ALLOWED_ERROR_VEHICLE_LENGTH_CM: f64 = 0.1;
/// let within_tolerance = (x - Y).abs() < ALLOWED_ERROR_VEHICLE_LENGTH_CM;
/// println!("{within_tolerance}"); // true
/// ```
///
/// NB! Do not use `f64::EPSILON` - while the error margin is often called "epsilon", this is
/// a different use of the term that is not suitable for floating point equality comparison.
/// Indeed, for the example above using `f64::EPSILON` as the allowed error would return `false`.
///
/// For the scenario where no meaningful absolute error can be defined, refer to
/// [the floating point guide](https://www.floating-point-gui.de/errors/comparison)
/// for a reference implementation of relative error based comparison of floating point values.
/// `MIN_NORMAL` in the reference implementation is equivalent to `MIN_POSITIVE` in Rust.
#[clippy::version = "pre 1.29.0"]
pub FLOAT_CMP_CONST,
restriction,
"using `==` or `!=` on float constants instead of comparing difference with an allowed error"
}

declare_clippy_lint! {
/// ### What it does
/// Checks for getting the remainder of integer division by one or minus
/// Checks for getting the remainder of a division by one or minus
/// one.
///
/// ### Why is this bad?
Expand Down Expand Up @@ -884,7 +821,6 @@ impl_lint_pass!(Operators => [
INTEGER_DIVISION,
CMP_OWNED,
FLOAT_CMP,
FLOAT_CMP_CONST,
MODULO_ONE,
MODULO_ARITHMETIC,
NEEDLESS_BITWISE_BOOL,
Expand Down
1 change: 1 addition & 0 deletions tests/ui/deprecated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
#![warn(clippy::wrong_pub_self_convention)]
#![warn(clippy::maybe_misused_cfg)]
#![warn(clippy::mismatched_target_os)]
#![warn(clippy::float_cmp_const)]

fn main() {}
8 changes: 7 additions & 1 deletion tests/ui/deprecated.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,11 @@ error: lint `clippy::mismatched_target_os` has been removed: this lint has been
LL | #![warn(clippy::mismatched_target_os)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 18 previous errors
error: lint `clippy::float_cmp_const` has been removed: `float_cmp` handles this via config options
--> tests/ui/deprecated.rs:23:9
|
LL | #![warn(clippy::float_cmp_const)]
| ^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 19 previous errors