use rustc_span::source_map::Spanned;
declare_clippy_lint! {
- /// **What it does:** Checks for statements of the form `(a - b) < f32::EPSILON` or
- /// `(a - b) < f64::EPSILON`. Notes the missing `.abs()`.
- ///
- /// **Why is this bad?** The code without `.abs()` is more likely to have a bug.
- ///
- /// **Known problems:** If the user can ensure that b is larger than a, the `.abs()` is
- /// technically unneccessary. However, it will make the code more robust and doesn't have any
- /// large performance implications. If the abs call was deliberately left out for performance
- /// reasons, it is probably better to state this explicitly in the code, which then can be done
- /// with an allow.
- ///
- /// **Example:**
- ///
- /// ```rust
- /// pub fn is_roughly_equal(a: f32, b: f32) -> bool {
- /// (a - b) < f32::EPSILON
- /// }
- /// ```
- /// Use instead:
- /// ```rust
- /// pub fn is_roughly_equal(a: f32, b: f32) -> bool {
- /// (a - b).abs() < f32::EPSILON
- /// }
- /// ```
+ /// ### What it does
+ /// Checks for statements of the form `(a - b) < f32::EPSILON` or
+ /// `(a - b) < f64::EPSILON`. Notes the missing `.abs()`.
+ ///
+ /// ### Why is this bad?
+ /// The code without `.abs()` is more likely to have a bug.
+ ///
+ /// ### Known problems
+ /// If the user can ensure that b is larger than a, the `.abs()` is
+ /// technically unnecessary. However, it will make the code more robust and doesn't have any
+ /// large performance implications. If the abs call was deliberately left out for performance
+ /// reasons, it is probably better to state this explicitly in the code, which then can be done
+ /// with an allow.
+ ///
+ /// ### Example
+ /// ```rust
+ /// pub fn is_roughly_equal(a: f32, b: f32) -> bool {
+ /// (a - b) < f32::EPSILON
+ /// }
+ /// ```
+ /// Use instead:
+ /// ```rust
+ /// pub fn is_roughly_equal(a: f32, b: f32) -> bool {
+ /// (a - b).abs() < f32::EPSILON
+ /// }
+ /// ```
+ #[clippy::version = "1.48.0"]
pub FLOAT_EQUALITY_WITHOUT_ABS,
- correctness,
+ suspicious,
"float equality check without `.abs()`"
}
if_chain! {
- // left hand side is a substraction
+ // left hand side is a subtraction
if let ExprKind::Binary(
Spanned {
node: BinOpKind::Sub,
if let Res::Def(DefKind::AssocConst, def_id) = cx.qpath_res(epsilon_path, rhs.hir_id);
if match_def_path(cx, def_id, &paths::F32_EPSILON) || match_def_path(cx, def_id, &paths::F64_EPSILON);
- // values of the substractions on the left hand side are of the type float
+ // values of the subtractions on the left hand side are of the type float
let t_val_l = cx.typeck_results().expr_ty(val_l);
let t_val_r = cx.typeck_results().expr_ty(val_r);
if let ty::Float(_) = t_val_l.kind();