Skip to content

Commit

Permalink
float_cmp: Don't lint self comparisons.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarcho committed Feb 13, 2024
1 parent 9039f50 commit 147961e
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 7 deletions.
9 changes: 7 additions & 2 deletions clippy_lints/src/operators/float_cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use clippy_utils::consts::{constant, Constant};
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::sugg::Sugg;
use clippy_utils::visitors::{for_each_expr, is_const_evaluatable};
use clippy_utils::{get_item_name, is_expr_named_const, peel_hir_expr_while, SpanlessEq};
use clippy_utils::{get_item_name, is_expr_named_const, path_res, peel_hir_expr_while, SpanlessEq};
use core::ops::ControlFlow;
use rustc_errors::Applicability;
use rustc_hir::def::Res;
use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, UnOp, Unsafety};
use rustc_lint::LateContext;
use rustc_middle::ty::{self, Ty, TypeFlags, TypeVisitableExt};
Expand Down Expand Up @@ -32,6 +33,10 @@ pub(crate) fn check<'tcx>(
&& !(matches!(left_red.kind, ExprKind::Lit(_)) && matches!(right_red.kind, ExprKind::Lit(_)))
// Allow comparing the results of signum()
&& !(is_signum(cx, left_red) && is_signum(cx, right_red))
&& match (path_res(cx, left_red), path_res(cx, right_red)) {
(Res::Err, _) | (_, Res::Err) => true,
(left, right) => left != right,
}
{
let left_c = constant(cx, cx.typeck_results(), left_red);
let is_left_const = left_c.is_some();
Expand Down Expand Up @@ -64,7 +69,7 @@ pub(crate) fn check<'tcx>(

if let Some(name) = get_item_name(cx, expr) {
let name = name.as_str();
if name == "eq" || name == "ne" || name == "is_nan" || name.starts_with("eq_") || name.ends_with("_eq") {
if name == "eq" || name == "ne" || name.starts_with("eq_") || name.ends_with("_eq") {
return;
}
}
Expand Down
11 changes: 11 additions & 0 deletions tests/ui-toml/float_cmp_change_detection/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//@no-rustfix

#![deny(clippy::float_cmp)]
#![allow(clippy::op_ref, clippy::eq_op)]

fn main() {
{
Expand All @@ -26,4 +27,14 @@ fn main() {
let _ = x == x + 1.0;
}
}
{
fn _f(x: f32) {
let _ = x == x;
let _ = x != x;
let _ = x == -x;
let _ = -x == x;
let _ = x as f64 == x as f64;
let _ = &&x == &&x;
}
}
}
4 changes: 2 additions & 2 deletions tests/ui-toml/float_cmp_change_detection/test.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: strict comparison of `f32` or `f64`
--> $DIR/test.rs:25:21
--> $DIR/test.rs:26:21
|
LL | let _ = x + 1.0 == x;
| ^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x + 1.0 - x).abs() < error_margin`
Expand All @@ -11,7 +11,7 @@ LL | #![deny(clippy::float_cmp)]
| ^^^^^^^^^^^^^^^^^

error: strict comparison of `f32` or `f64`
--> $DIR/test.rs:26:21
--> $DIR/test.rs:27:21
|
LL | let _ = x == x + 1.0;
| ^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x - (x + 1.0)).abs() < error_margin`
Expand Down
11 changes: 11 additions & 0 deletions tests/ui-toml/float_cmp_constant_comparisons/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//@no-rustfix

#![deny(clippy::float_cmp)]
#![allow(clippy::op_ref, clippy::eq_op)]

fn main() {
{
Expand All @@ -26,4 +27,14 @@ fn main() {
let _ = x == x + 1.0;
}
}
{
fn _f(x: f32) {
let _ = x == x;
let _ = x != x;
let _ = x == -x;
let _ = -x == x;
let _ = x as f64 == x as f64;
let _ = &&x == &&x;
}
}
}
2 changes: 1 addition & 1 deletion tests/ui-toml/float_cmp_constant_comparisons/test.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: strict comparison of `f32` or `f64`
--> $DIR/test.rs:16:17
--> $DIR/test.rs:17:17
|
LL | let _ = f(1.0) == f(2.0);
| ^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f(1.0) - f(2.0)).abs() < error_margin`
Expand Down
11 changes: 11 additions & 0 deletions tests/ui-toml/float_cmp_named_constants/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//@no-rustfix

#![deny(clippy::float_cmp)]
#![allow(clippy::op_ref, clippy::eq_op)]

fn main() {
{
Expand All @@ -26,4 +27,14 @@ fn main() {
let _ = x == x + 1.0;
}
}
{
fn _f(x: f32) {
let _ = x == x;
let _ = x != x;
let _ = x == -x;
let _ = -x == x;
let _ = x as f64 == x as f64;
let _ = &&x == &&x;
}
}
}
2 changes: 1 addition & 1 deletion tests/ui-toml/float_cmp_named_constants/test.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: strict comparison of `f32` or `f64`
--> $DIR/test.rs:9:21
--> $DIR/test.rs:10:21
|
LL | let _ = x == C;
| ^^^^^^ help: consider comparing them within some margin of error: `(x - C).abs() < error_margin`
Expand Down
3 changes: 3 additions & 0 deletions tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ error: error reading Clippy's configuration file: unknown field `allow_mixed_uni
enum-variant-name-threshold
enum-variant-size-threshold
excessive-nesting-threshold
float-cmp-ignore-change-detection
float-cmp-ignore-constant-comparisons
float-cmp-ignore-named-constants
future-size-threshold
ignore-interior-mutability
large-error-threshold
Expand Down
14 changes: 13 additions & 1 deletion tests/ui/float_cmp.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//@no-rustfix

#![warn(clippy::float_cmp)]
#![allow(clippy::op_ref)]
#![allow(clippy::op_ref, clippy::eq_op)]

fn main() {
{
Expand Down Expand Up @@ -330,4 +330,16 @@ fn main() {
//~^ ERROR: strict comparison of `f32` or `f64`
}
}

// Self comparisons
{
fn _f(x: f32) {
let _ = x == x;
let _ = x != x;
let _ = x == -x;
let _ = -x == x;
let _ = x as f64 == x as f64;
let _ = &&x == &&x;
}
}
}

0 comments on commit 147961e

Please sign in to comment.