llvm / llvm-project

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

[Clang-Tidy] Create bugprone-accuracy-lost-in-floating-point-calculation check #80858

Open PiotrZSL opened 9 months ago

PiotrZSL commented 9 months ago

I'm adding this here so, i wouldn't forget.

int test(int a)
{
   return int((double)a * 0.01); // could be done as a/100;
}

Example not the best but, idea is to find all expressions (sometimes multiline) where input is an integer and output is an integer, but calculation is done on floating-point. Sometimes by changing order of operation entire calculation could be done fully on integers.

Check could have also other names.

llvmbot commented 9 months ago

@llvm/issue-subscribers-clang-tidy

Author: Piotr Zegar (PiotrZSL)

I'm adding this here so, i wouldn't forget. ``` int test(int a) { return int((double)a * 0.01); // could be done as a/100; } ``` Example not the best but, idea is to find all expressions (sometimes multiline) where input is an integer and output is an integer, but calculation is done on floating-point. Sometimes by changing order of operation entire calculation could be done fully on integers. Check could have also other names.
torshepherd commented 9 months ago

I think this could be expanded to bugprone-round-trip-conversions - is that the meaning of the check? If so this would be most powerful with the full static analyzer to check across function boundaries. I suspect this could be really useful in that case