Skip to content

clippy::suboptimal_flops suggestion can introduce errors with mul_add() #11831

@StHagel

Description

@StHagel

Summary

The clippy::suboptimal_flops lint often suggests to use builtin functions such as mul_add() to make code more performant, but this can lead to errors when using the Complex64 type from the num crate together with f64 values.

Reproducer

I tried this code:

use num::complex::Complex64;

fn main() {
    let a = Complex64::new(1.0, 1.0);
    let b = 1.0_f64;
    let c = 1.0;
    
    let d = a + b * c;
    
    println!("{d:?}");
}

clippy suggests to rewrite this as:

use num::complex::Complex64;

fn main() {
    let a = Complex64::new(1.0, 1.0);
    let b = 1.0_f64;
    let c = 1.0;
    
    let d = b.mul_add(c, a);
    
    println!("{d:?}");
}

This causes the compiler error:

error[E0308]: mismatched types
 --> src/main.rs:8:26
  |
8 |     let d = b.mul_add(c, a);
  |               -------    ^ expected `f64`, found `Complex<f64>`
  |               |
  |               arguments to this method are incorrect
  |
  = note: expected type `f64`
           found struct `Complex<f64>`
note: method defined here
 --> /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library/std/src/f64.rs:270:12

For more information about this error, try `rustc --explain E0308`.

Version

rustc 1.74.0 (79e9716c9 2023-11-13)
binary: rustc
commit-hash: 79e9716c980570bfd1f666e3b16ac583f0168962
commit-date: 2023-11-13
host: x86_64-unknown-linux-gnu
release: 1.74.0
LLVM version: 17.0.4

Additional Labels

I-suggestion-causes-error

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions