Skip to content

Commit 147961e

Browse files
committed
float_cmp: Don't lint self comparisons.
1 parent 9039f50 commit 147961e

File tree

9 files changed

+60
-7
lines changed

9 files changed

+60
-7
lines changed

clippy_lints/src/operators/float_cmp.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ use clippy_utils::consts::{constant, Constant};
22
use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::sugg::Sugg;
44
use clippy_utils::visitors::{for_each_expr, is_const_evaluatable};
5-
use clippy_utils::{get_item_name, is_expr_named_const, peel_hir_expr_while, SpanlessEq};
5+
use clippy_utils::{get_item_name, is_expr_named_const, path_res, peel_hir_expr_while, SpanlessEq};
66
use core::ops::ControlFlow;
77
use rustc_errors::Applicability;
8+
use rustc_hir::def::Res;
89
use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, UnOp, Unsafety};
910
use rustc_lint::LateContext;
1011
use rustc_middle::ty::{self, Ty, TypeFlags, TypeVisitableExt};
@@ -32,6 +33,10 @@ pub(crate) fn check<'tcx>(
3233
&& !(matches!(left_red.kind, ExprKind::Lit(_)) && matches!(right_red.kind, ExprKind::Lit(_)))
3334
// Allow comparing the results of signum()
3435
&& !(is_signum(cx, left_red) && is_signum(cx, right_red))
36+
&& match (path_res(cx, left_red), path_res(cx, right_red)) {
37+
(Res::Err, _) | (_, Res::Err) => true,
38+
(left, right) => left != right,
39+
}
3540
{
3641
let left_c = constant(cx, cx.typeck_results(), left_red);
3742
let is_left_const = left_c.is_some();
@@ -64,7 +69,7 @@ pub(crate) fn check<'tcx>(
6469

6570
if let Some(name) = get_item_name(cx, expr) {
6671
let name = name.as_str();
67-
if name == "eq" || name == "ne" || name == "is_nan" || name.starts_with("eq_") || name.ends_with("_eq") {
72+
if name == "eq" || name == "ne" || name.starts_with("eq_") || name.ends_with("_eq") {
6873
return;
6974
}
7075
}

tests/ui-toml/float_cmp_change_detection/test.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@no-rustfix
22

33
#![deny(clippy::float_cmp)]
4+
#![allow(clippy::op_ref, clippy::eq_op)]
45

56
fn main() {
67
{
@@ -26,4 +27,14 @@ fn main() {
2627
let _ = x == x + 1.0;
2728
}
2829
}
30+
{
31+
fn _f(x: f32) {
32+
let _ = x == x;
33+
let _ = x != x;
34+
let _ = x == -x;
35+
let _ = -x == x;
36+
let _ = x as f64 == x as f64;
37+
let _ = &&x == &&x;
38+
}
39+
}
2940
}

tests/ui-toml/float_cmp_change_detection/test.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: strict comparison of `f32` or `f64`
2-
--> $DIR/test.rs:25:21
2+
--> $DIR/test.rs:26:21
33
|
44
LL | let _ = x + 1.0 == x;
55
| ^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x + 1.0 - x).abs() < error_margin`
@@ -11,7 +11,7 @@ LL | #![deny(clippy::float_cmp)]
1111
| ^^^^^^^^^^^^^^^^^
1212

1313
error: strict comparison of `f32` or `f64`
14-
--> $DIR/test.rs:26:21
14+
--> $DIR/test.rs:27:21
1515
|
1616
LL | let _ = x == x + 1.0;
1717
| ^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x - (x + 1.0)).abs() < error_margin`

tests/ui-toml/float_cmp_constant_comparisons/test.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@no-rustfix
22

33
#![deny(clippy::float_cmp)]
4+
#![allow(clippy::op_ref, clippy::eq_op)]
45

56
fn main() {
67
{
@@ -26,4 +27,14 @@ fn main() {
2627
let _ = x == x + 1.0;
2728
}
2829
}
30+
{
31+
fn _f(x: f32) {
32+
let _ = x == x;
33+
let _ = x != x;
34+
let _ = x == -x;
35+
let _ = -x == x;
36+
let _ = x as f64 == x as f64;
37+
let _ = &&x == &&x;
38+
}
39+
}
2940
}

tests/ui-toml/float_cmp_constant_comparisons/test.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: strict comparison of `f32` or `f64`
2-
--> $DIR/test.rs:16:17
2+
--> $DIR/test.rs:17:17
33
|
44
LL | let _ = f(1.0) == f(2.0);
55
| ^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f(1.0) - f(2.0)).abs() < error_margin`

tests/ui-toml/float_cmp_named_constants/test.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@no-rustfix
22

33
#![deny(clippy::float_cmp)]
4+
#![allow(clippy::op_ref, clippy::eq_op)]
45

56
fn main() {
67
{
@@ -26,4 +27,14 @@ fn main() {
2627
let _ = x == x + 1.0;
2728
}
2829
}
30+
{
31+
fn _f(x: f32) {
32+
let _ = x == x;
33+
let _ = x != x;
34+
let _ = x == -x;
35+
let _ = -x == x;
36+
let _ = x as f64 == x as f64;
37+
let _ = &&x == &&x;
38+
}
39+
}
2940
}

tests/ui-toml/float_cmp_named_constants/test.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: strict comparison of `f32` or `f64`
2-
--> $DIR/test.rs:9:21
2+
--> $DIR/test.rs:10:21
33
|
44
LL | let _ = x == C;
55
| ^^^^^^ help: consider comparing them within some margin of error: `(x - C).abs() < error_margin`

tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ error: error reading Clippy's configuration file: unknown field `allow_mixed_uni
202202
enum-variant-name-threshold
203203
enum-variant-size-threshold
204204
excessive-nesting-threshold
205+
float-cmp-ignore-change-detection
206+
float-cmp-ignore-constant-comparisons
207+
float-cmp-ignore-named-constants
205208
future-size-threshold
206209
ignore-interior-mutability
207210
large-error-threshold

tests/ui/float_cmp.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//@no-rustfix
22

33
#![warn(clippy::float_cmp)]
4-
#![allow(clippy::op_ref)]
4+
#![allow(clippy::op_ref, clippy::eq_op)]
55

66
fn main() {
77
{
@@ -330,4 +330,16 @@ fn main() {
330330
//~^ ERROR: strict comparison of `f32` or `f64`
331331
}
332332
}
333+
334+
// Self comparisons
335+
{
336+
fn _f(x: f32) {
337+
let _ = x == x;
338+
let _ = x != x;
339+
let _ = x == -x;
340+
let _ = -x == x;
341+
let _ = x as f64 == x as f64;
342+
let _ = &&x == &&x;
343+
}
344+
}
333345
}

0 commit comments

Comments
 (0)