rust-lang / rust-lang/rust-clippy

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

Open
#14,366 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the shown (expr_with_float_literals as f64).powf(2.0) case and inspect the unnecessary_cast lint and its suggestion. Done means the lint no longer emits a suggestion that causes an ambiguous numeric type compile error, with regression coverage for this expression.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.