Issue 76311
Summary `bugprone-implicit-widening-of-multiplication-result` does not properly cast operations
Labels new issue
Assignees
Reporter felix642
    The check `bugprone-implicit-widening-of-multiplication-result` does not suggest the proper usage of parenthesis when adding casts to multiplications when the result is stored in a wider type.

If we take for example the following code :
```
uint64_t f() {
  return 100000U * 100000U * 1U;
}
```
Clang tidy will suggest to cast the result of the first multiplication to uint64_t since this is what the function returns.
```
uint64_t f() {
  return static_cast<uint64_t>(100000U * 100000U) * 1U;
}
```

This is wrong as it does not properly fix the issue. The multiplication will still overflow and the method will return the wrong value. Instead, the check should suggest to cast the first member of the operations to make sure that we keep the desired precision.

```
uint64_t f() {
  return static_cast<uint64_t>(100000U) * 100000U * 1U;
}
```

Godbolt: https://godbolt.org/z/a48zh7Wb5
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to