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

Float comparison lint doesn't seem to trigger on float array comparison #4277

Closed
Lokathor opened this issue Jul 15, 2019 · 13 comments · Fixed by #5345
Closed

Float comparison lint doesn't seem to trigger on float array comparison #4277

Lokathor opened this issue Jul 15, 2019 · 13 comments · Fixed by #5345
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy

Comments

@Lokathor
Copy link

#[test]
fn m128_max() {
  let a: m128 = cast([5.0_f32, 6.0, 7.0, 8.5]);
  let b: m128 = cast([-8.0_f32, 4.0, 12.0, 0.5]);
  let out: [f32; 4] = cast(a.max(b));
  assert_eq!(out, [5.0_f32, 6.0, 12.0, 8.5]);
  assert_eq!(out[0], [5.0_f32, 6.0, 12.0, 8.5][0]);
}

the second assert triggers clippy, the first doesn't.

@flip1995 flip1995 added good-first-issue These issues are a good way to get started with Clippy C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages labels Jul 16, 2019
@briankabiro
Copy link
Contributor

Hi, I would like to take a look at this.

@flip1995
Copy link
Member

Great! If you have any questions, don't hesitate to ask.

@briankabiro
Copy link
Contributor

Cool, I am going to go through it and make sure to reach out. :)

@briankabiro
Copy link
Contributor

Hi, @flip1995. I took the time to go through the codebase and think that the code change should be somewhere in the misc.rs

I am not sure why it wouldn't trigger when comparing array values. Could you offer some pointers?

@flip1995
Copy link
Member

You're on the right track. In the line you linked is a check for the left and right side of the Eq/Ne Binop if both sides are floats:

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

This function takes the expression, "derefs" it, and checks if the type is float. (i.e is_float(&&float) = is_float(float) = true, but is_float([float; N]) = false). So you have to modify this function, so that it not only checks the expressions type to be ty::Floats, but also for ty::Array(ty::Float, _)s.

Hope this helps :)

@briankabiro
Copy link
Contributor

briankabiro commented Jul 31, 2019

Yes, this definitely helps. I think I can work with this. Thanks!

briankabiro added a commit to briankabiro/rust-clippy that referenced this issue Jul 31, 2019
briankabiro added a commit to briankabiro/rust-clippy that referenced this issue Aug 6, 2019
briankabiro added a commit to briankabiro/rust-clippy that referenced this issue Aug 13, 2019
briankabiro added a commit to briankabiro/rust-clippy that referenced this issue Aug 27, 2019
briankabiro added a commit to briankabiro/rust-clippy that referenced this issue Sep 17, 2019
@briankabiro
Copy link
Contributor

Hey @flip1995. I've been caught up with work and this slipped under my radar; apologies for not giving an update sooner. I'd be happy to let someone else finish this if there's someone that you might recommend.

If not, I'd still be interested in completing the feature. 😃

@flip1995
Copy link
Member

No problem, you can get back to it, once you have more time 👍

@briankabiro
Copy link
Contributor

Thanks for understanding :)

@marcin-serwin
Copy link
Contributor

Hi, I'd like to take up this issue if possible.

@flip1995
Copy link
Member

flip1995 commented Mar 9, 2020

Yeah sure, you can take a look at #4343 for some inspiration.

@briankabiro
Copy link
Contributor

Hey @Toxyxer. Go right ahead. I'm interested in seeing the solution that you come up with!

@marcin-serwin
Copy link
Contributor

marcin-serwin commented Mar 20, 2020

Hey @flip1995, I've opened a pull request with my proposed changes. Some comments:

I think this example should trigger the lint because the arrays are declared with let, so we don't know whether they are all zeros at compile-time (unless there is another reason why we know this). Similarly code

    let x = 0.0;
    let y = 0.0;
    x == y;

triggers the lint. I've changed the code so that when we change the code in the example to declare arrays with consts it will pass.

I've noticed that the assert_eq macro with floats triggers the lint regardless if they are equal to zeros as evidenced by:

error: strict comparison of `f32` or `f64`
  --> $DIR/float_cmp.rs:47:5
   |
LL |     assert_eq!(0.0, 0.0);
   |     ^^^^^^^^^^^^^^^^^^^^^
   |

so I didn't use it in tests. It's a separate issue so I didn't do anything about it. It's already reported here: #3804.

I'm new to rust so there is probably room for improvement in the submitted code. Thanks in advance for any suggestions on how to make it better.

flip1995 added a commit to flip1995/rust-clippy that referenced this issue Apr 7, 2020
…comparison, r=flip1995

Add lint for float in array comparison

Fixes rust-lang#4277
changelog:
- Added new handler for expression of index kind (e.g. `arr[i]`). It returns a constant when both array and index are constant, or when the array is constant and all values are equal.
- Trigger float_cmp and float_cmp_const lint when comparing arrays. Allow for comparison when one of the arrays contains only zeros or infinities.
- Added appropriate tests for such cases.
bors added a commit that referenced this issue Apr 11, 2020
… r=flip1995

Add lint for float in array comparison

Fixes #4277
changelog:
- Added new handler for expression of index kind (e.g. `arr[i]`). It returns a constant when both array and index are constant, or when the array is constant and all values are equal.
- Trigger float_cmp and float_cmp_const lint when comparing arrays. Allow for comparison when one of the arrays contains only zeros or infinities.
- Added appropriate tests for such cases.
bors added a commit that referenced this issue Apr 15, 2020
… r=flip1995

Add lint for float in array comparison

Fixes #4277
changelog:
- Added new handler for expression of index kind (e.g. `arr[i]`). It returns a constant when both array and index are constant, or when the array is constant and all values are equal.
- Trigger float_cmp and float_cmp_const lint when comparing arrays. Allow for comparison when one of the arrays contains only zeros or infinities.
- Added appropriate tests for such cases.
@bors bors closed this as completed in a96f874 Apr 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants