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

Add lint for comparing floats in an array #4343

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion clippy_lints/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,13 @@ fn is_signum(cx: &LateContext<'_, '_>, expr: &Expr) -> bool {
}

fn is_float(cx: &LateContext<'_, '_>, expr: &Expr) -> bool {
matches!(walk_ptrs_ty(cx.tables.expr_ty(expr)).sty, ty::Float(_))
let value = &walk_ptrs_ty(cx.tables.expr_ty(expr)).sty;

if let ty::Array(arr_ty, _) = value {
return matches!(arr_ty.sty, ty::Float(_));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we also have to walk_ptrs_ty on arr_ty, so that Arrays of &floats also get linted.

Copy link
Contributor Author

@briankabiro briankabiro Sep 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On it 🏃

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm having a bit of an issue getting the walk_ptrs_ty to work with arr_ty; I have updated the tests in the meanwhile. You can have a look if I'm going in the right direction so far.

};

matches!(value, ty::Float(_))
}

fn check_to_owned(cx: &LateContext<'_, '_>, expr: &Expr, other: &Expr) {
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/float_cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ fn main() {

assert_eq!(a, b); // no errors

let a1: [f32; 1] = [0.0];
let a2: [f32; 1] = [1.1];
Copy link
Member

@flip1995 flip1995 Sep 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add a test for the type [&f32; 1]?

Playground

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the help.


assert_eq!(a1[0], a2[0]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to do it right, this should not trigger the lint, since everything in a1 ist constant 0.0, so a comparison is ok here.


On a more general note, this enhancement needs more tests like the base case of comparing floats. (Comparing arrays of constant 0.0, constant {NEG_}INFINITY, ...)


assert_eq!(&a1[0], &a2[0]);

// no errors - comparing signums is ok
let x32 = 3.21f32;
1.23f32.signum() == x32.signum();
Expand Down
28 changes: 27 additions & 1 deletion tests/ui/float_cmp.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,31 @@ note: std::f32::EPSILON and std::f64::EPSILON are available.
LL | twice(x) != twice(ONE as f64);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 3 previous errors
error: strict comparison of f32 or f64
--> $DIR/float_cmp.rs:83:5
|
LL | assert_eq!(a1[0], a2[0]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: std::f32::EPSILON and std::f64::EPSILON are available.
--> $DIR/float_cmp.rs:83:5
|
LL | assert_eq!(a1[0], a2[0]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: strict comparison of f32 or f64
--> $DIR/float_cmp.rs:85:5
|
LL | assert_eq!(&a1[0], &a2[0]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: std::f32::EPSILON and std::f64::EPSILON are available.
--> $DIR/float_cmp.rs:85:5
|
LL | assert_eq!(&a1[0], &a2[0]);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: aborting due to 5 previous errors