<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/76311>76311</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            `bugprone-implicit-widening-of-multiplication-result` does not properly cast operations
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          felix642
      </td>
    </tr>
</table>

<pre>
    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
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0VMFu4zYQ_RrqMrBBU7IkH3TIJnWx9130uKDEkcSGJgXOKE726wtKStwEObRF1zBoS5x58-bNIzWRHTxiI45fxPEh0zOPITY9OvtcFiprg3lpvo0I3YjdI4hStvMwxeBxZy-Ts53l3dUa9NYPu9DvLrNjm95rtsHvItLsWJQSTEACHxhoHgYkBh4RphgmjDCTHhBCD5OO6HlEsgTXET1oY6wfoNPEBBzgPfwWlJDWQmAJiENEA9aDhsQsAr9MuBfyQci7df3awxWB9SNCHyLgs75MDhecPjgXrkvNYBBEvqWIUm7f5XG2nsviB0MvVC3UCUT1Zd0BiMhz9HCQ6fMdhLp7__-7yLdYUT18in7vtB-ArXmBq3XuJllYlPh7w6FfadtI_EGdFP7Gk6zvUoeWYNFWryj97LsldiVN-__aLbFm2_1I9ER-_5og8t-Eqj9RYgH5Z2Ks67dX5jH4ATSB5ZulVhu5F-jt89KWJZpxD8m3HzRZ9eS0hieMvQtX0N4sWRfkMZg1ZGsrvV5LPmmXIL96YtRGqPtlbz0VNIbZmU_HtA0GLy3G12Elspt_k6WTDWmOaTqakzEfEacl0CDZ5OUpYmfJBv_Oxb9kSG-D-XeOXdffg2mDY5Hfwcg8UTo96izUeVg39iEOQp1_CnXWRf1zrP5oj5lpcnPKTzrD5lDJPD_JQh6zsam6g9R1WeZ1VZeVrMuu19XRtEodu2NRV5ltlFT5QalCFlLKYt_XUtZ1q-XhVBe6z0Uh8aKt2zv3dEm1s8UXTVXmh0PmdIuOlntPKY_X1TRCqXQNxibl7Np5IFFIZ4nphsKWHTb_w1X45tvFLjdbZHN0zQcFLY9zu-_CRahzYrL97KYY_sSOhTov_Emo89LfXwEAAP__2uHbwg">