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

unecessary cast false positive #13878

Open
arifd opened this issue Dec 26, 2024 · 2 comments
Open

unecessary cast false positive #13878

arifd opened this issue Dec 26, 2024 · 2 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@arifd
Copy link

arifd commented Dec 26, 2024

Summary

This code will fail to compile:

fn main() {
    let n: f64 = 1.0;
    let _: f64 = (0.05).min(n);
}

making a cast necessary (or preferable to 0.05f64 IMO).

Writing this code, will compile, but causes clippy to warn:

fn main() {
    let n: f64 = 1.0;
    let _: f64 = (0.05 as f64).min(n);
}

I suspect it might be really difficult to solve this in a non-hacky way, but thought I would create this issue to at least raise awareness.

Lint Name

unecessary cast

Reproducer

No response

Version

rust playground stable 1.83.0: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=115441b5376a81c65d1cd21c2d86ac6d

Additional Labels

No response

@arifd arifd added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Dec 26, 2024
@Centri3
Copy link
Member

Centri3 commented Dec 26, 2024

I suspect it might be really difficult to solve this in a non-hacky way

Seems doable 👍 Just need to check that the parent expression isn't one of the shared float methods (though trait methods would be fine here afaik. Just checking the name isn't 100% thorough!)

^- Actually, thought this was initially about the suggestion being incorrect; so this is more subjective. I do think 0.05f64 is right.

@arifd
Copy link
Author

arifd commented Dec 26, 2024

Yes, to be clear 0.05f64 will (correctly) silence the clippy warning, but I think as f64 should also silence it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

2 participants