Skip to content

Commit aed5751

Browse files
committed
float_cmp: Don't lint self comparisons.
1 parent 93f75b9 commit aed5751

File tree

9 files changed

+61
-8
lines changed

9 files changed

+61
-8
lines changed

clippy_lints/src/operators/float_cmp.rs

+10-3
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_without_closures, 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, Safety, UnOp};
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_reduced.kind, ExprKind::Lit(_)) && matches!(right_reduced.kind, ExprKind::Lit(_)))
3334
// Allow comparing the results of signum()
3435
&& !(is_signum(cx, left_reduced) && is_signum(cx, right_reduced))
36+
&& match (path_res(cx, left_reduced), path_res(cx, right_reduced)) {
37+
(Res::Err, _) | (_, Res::Err) => true,
38+
(left, right) => left != right,
39+
}
3540
{
3641
let left_c = constant(cx, cx.typeck_results(), left_reduced);
3742
let is_left_const = left_c.is_some();
@@ -51,7 +56,9 @@ pub(crate) fn check<'tcx>(
5156
return;
5257
}
5358

54-
if config.ignore_named_constants && (is_expr_named_const(cx, left_reduced) || is_expr_named_const(cx, right_reduced)) {
59+
if config.ignore_named_constants
60+
&& (is_expr_named_const(cx, left_reduced) || is_expr_named_const(cx, right_reduced))
61+
{
5562
return;
5663
}
5764

@@ -64,7 +71,7 @@ pub(crate) fn check<'tcx>(
6471

6572
if let Some(name) = get_item_name(cx, expr) {
6673
let name = name.as_str();
67-
if name == "eq" || name == "ne" || name == "is_nan" || name.starts_with("eq_") || name.ends_with("_eq") {
74+
if name == "eq" || name == "ne" || name.starts_with("eq_") || name.ends_with("_eq") {
6875
return;
6976
}
7077
}

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-
--> tests/ui-toml/float_cmp_change_detection/test.rs:25:21
2+
--> tests/ui-toml/float_cmp_change_detection/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-
--> tests/ui-toml/float_cmp_change_detection/test.rs:26:21
14+
--> tests/ui-toml/float_cmp_change_detection/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-
--> tests/ui-toml/float_cmp_constant_comparisons/test.rs:16:17
2+
--> tests/ui-toml/float_cmp_constant_comparisons/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-
--> tests/ui-toml/float_cmp_named_constants/test.rs:9:21
2+
--> tests/ui-toml/float_cmp_named_constants/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

+1
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ error: error reading Clippy's configuration file: unknown field `allow_mixed_uni
216216
enum-variant-name-threshold
217217
enum-variant-size-threshold
218218
excessive-nesting-threshold
219+
float-cmp-ignore-change-detection
219220
float-cmp-ignore-constant-comparisons
220221
float-cmp-ignore-named-constants
221222
future-size-threshold

tests/ui/float_cmp.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// FIXME(f16_f128): const casting is not yet supported for these types. Add when available.
44

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

88
fn main() {
99
{
@@ -340,4 +340,16 @@ fn main() {
340340
let mut f = |y: f32| -> f32 { core::mem::replace(&mut x, y) };
341341
let _ = f(1.0) == f(1.0) + 1.0; //~ float_cmp
342342
}
343+
344+
// Self comparisons
345+
{
346+
fn _f(x: f32) {
347+
let _ = x == x;
348+
let _ = x != x;
349+
let _ = x == -x;
350+
let _ = -x == x;
351+
let _ = x as f64 == x as f64;
352+
let _ = &&x == &&x;
353+
}
354+
}
343355
}

0 commit comments

Comments
 (0)