llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.57k stars 11.32k forks source link

[clang-tidy][request] Detect floating-point casts that cause loss of precision #97809

Open chrchr-github opened 1 month ago

chrchr-github commented 1 month ago
double f(double a, double b, float c) {
    return a + (float)b + c;
}

The result of the cast is used as double, so it is unlikely to be intentional. There should be no warning when the cast affects overload resolution though (if that is even possible).

firewave commented 1 month ago

Looks like this is already coved by -Wdouble-promotion.

<source>:2:27: warning: implicit conversion increases floating-point precision: 'float' to 'double' [-Wdouble-promotion]
    2 |     return a + (float)b + c;
      |                         ~ ^
<source>:2:16: warning: implicit conversion increases floating-point precision: 'float' to 'double' [-Wdouble-promotion]
    2 |     return a + (float)b + c;
      |              ~ ^~~~~~~~

https://godbolt.org/z/edfMs737P

chrchr-github commented 1 month ago

-Wdouble-promotion warns about the implicit conversion to double, but not about the nonsensical explicit cast to float.