rust-lang / rust-lang/rust-clippy

Clippy suggests code that does not compile, because {float} is an ambigous type

Open
#7,652 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

fn main() {
    let x = (1. / 2.) * 2_f64 + 3_f64;
}

cargo clippy -- -W clippy::nursery

warning: multiply and add expressions can be calculated more efficiently and accurately
 --> src/main.rs:2:13
  |
2 |     let x = (1. / 2.) * 2_f64 + 3_f64;
  |             ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(1. / 2.).mul_add(2_f64, 3_f64)`
  |
  = note: `-W clippy::suboptimal-flops` implied by `-W clippy::nursery`
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#suboptimal_flops

If we do what clippy suggests, we get a compile error:

error[E0689]: can't call method `mul_add` on ambiguous numeric type `{float}`
 --> src/main.rs:2:23
  |
2 |     let x = (1. / 2.).mul_add(2_f64, 3_f64);
  |                       ^^^^^^^
rustc 1.55.0-nightly (ce1d5611a 2021-06-18)
binary: rustc
commit-hash: ce1d5611a28468663e275078649e7ca6eef735a8
commit-date: 2021-06-18
host: x86_64-unknown-linux-gnu
release: 1.55.0-nightly
LLVM version: 12.0.1

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

Reproduce the report with the shown Rust example by running cargo clippy -- -W clippy::nursery. Start with the suboptimal_flops lint and inspect how it selects the mul_add suggestion. Done means the suggested replacement compiles for this expression or the lint no longer suggests invalid code.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.