Skip to content

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

Open
@trevoranderson

Description

@trevoranderson

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions