Skip to content

unnecessary_cast fix suggestion introduces compile error for (expr_with_float_literals as f64).powf(2.0) #14366

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

Open
trevoranderson opened this issue Mar 6, 2025 · 0 comments · May be fixed by #14492
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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@trevoranderson
Copy link

Summary

The following code triggers the lint:

((1.0 / 8.0) as f64).powf(i as f64)
^^^^^^^^^^^^^^^^^^^^ help: try: `(1.0 / 8.0)`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast
    = note: `-D clippy::unnecessary-cast` implied by `-D warnings`
    = help: to override `-D warnings` add `#[allow(clippy::unnecessary_cast)]`

The problem is that when the code is "fixed" according to the suggestion ((1.0 / 8.0).powf(i as f64)), the rust compiler gives the following error

 can't call method `powf` on ambiguous numeric type `{float}`

so it's not really an unnecessary cast, since the compiler needs the type information for powf. I can work around it by doing 1.0f64 / 8.0f64 to avoid needing to cast while still having sufficient information to allow powf to disambiguate

Reproducer

No response

Version

rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: x86_64-apple-darwin
release: 1.85.0
LLVM version: 19.1.7

Additional Labels

No response

@trevoranderson trevoranderson added the C-bug Category: Clippy is not doing the correct thing label Mar 6, 2025
@samueltardieu samueltardieu added I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied I-false-positive Issue: The lint was triggered on code it shouldn't have labels Mar 6, 2025
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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants