Hacker News new | ask | show | jobs
by tasn 2000 days ago
Thanks a lot for the suggestion, I can't believe I didn't know about this. However I just tried it and I can't get it to work.

I added this to the top of https://github.com/etesync/etebase-rs/blob/master/src/lib.rs and then ran `cargo clippy`

    #![warn(clippy::all)]

    // Should fail https://rust-lang.github.io/rust-clippy/master/index.html#float_cmp
    pub fn bool_test(x: f32, y: f32) -> bool {
        x == y
    }
Any idea what's missing? Why is it not failing?

Edit: I know the above example is bad code, that's the point. I want clippy to complain about it but it doesn't.

4 comments

Does it work if you run `cargo clean` and then `cargo clippy` again? Clippy runs it's lints in an early pass of the compiler/checker. Many IDEs/editors will automatically `cargo check` under the hood to grab errors. Then when you run `cargo clippy`, that part of the compilation is already cached and so clippy doesn't give you any output :(

To my knowledge (it's been a while since I looked) fixing this behavior is blocked on cargo stabilizing something and has been for literal years.

That point of frustration aside, it's worth it...Clippy is an absolutely amazing piece of software. Both for pedagogy and normal development.

EDIT: Just dug up the issue. If you're on nightly you can use `cargo clippy -Z unstable-options` to avoid the clean/rebuild. Hopefully stuff gets stabilized soon. Here's the issue for reference: https://github.com/rust-lang/rust-clippy/issues/4612

That fixed it, thanks a lot!
Instead of `cargo clean`, `touch src/main.rs` or `touch src/lib.rs` (or actually touching any source file and thereby changing the `last modified` date to now) will have the same effect. That's what I've been using.
I’m not sure and can’t check at the moment; but maybe the functions not called so it’s being ignored?
It's public in my lib, so it's exported. Though I also tried calling it from my code, it still doesn't trigger a warning. :|
Comparing floats by equality is a dangerous pattern. It's easy for small precision errors to occur. You should instead check that they are close enough to each other, using an epsilon that you find appropriate, perhaps 1e-10. `(x - y).abs() < epsilon` should do the trick
I know, it's an example I was hoping clippy would catch in order for it to fail so I know it works. Read what I wrote...
It's in fact the dangerous pattern used to illustrate the linter under discussion here.
Float comparison is an anti-pattern. Use an epsilon instead. https://stackoverflow.com/questions/4915462/how-should-i-do-...
I know, it's an example I was hoping clippy would catch in order for it to fail so I know it works. Read what I wrote...