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

    <tr>
        <th>Summary</th>
        <td>
            [clang-tidy] false positives on check "bugprone-implicit-widening-of-multiplication-result"
        </td>
    </tr>

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

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

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

<pre>
    Hi,

I like this check:
    https://clang.llvm.org/extra/clang-tidy/checks/bugprone/implicit-widening-of-multiplication-result.html
Though in my code base over half of the reported cases are false positives.

I've prepared some minimal example to demonstrate the problem:

// https://godbolt.org/z/6M4Kj1sd6
//--------------------------------------------
int constant(int* p)
{
    return p[3 * 4];
}

int in_range(int*p, unsigned char u8)
{
    return p[0x4000 * u8];
}
//--------------------------------------------

In the first function the multiplication is done on constants (could be integer literals or constexpr variables). The calculated index remains the same whether we do the multiplication before or after the widening.

The second function is similar: whatever the value of the 'u8' variable (0..255), the result of the multiplication will never overflow, and thus the order of the widening and multiplication operations doesn't make a difference.

I have no idea how difficult it would be to extend clang-tidy to recognize and filter these cases (possibly behind an option). But it would be useful to me. Thanks!
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJydVdGO2joQ_ZrwYoGSQAh5yEO3q6rV1X3re-XEEzKtYyPbAbZf32MDuwuq1N6LIBh7PHPOzJmhs-ql_cxZ-THLn7P8w-X5RWj-QSKM7EU_Uv8jW19PBF5jCAcfd8pPePdamv1K6-O0sm6PDToHJ28Hy8DqJf6IXjwW3bw_OGsIS54OmnsOyxMrMgxjOyynWQeO-zKwNUtHHhurMUz6AuDraOf9KNiI6UX0VpHopCdhj-TEKPUg7ADcJBwdrAukRI9jL6QjMUgNy4P1HPhIfnXHOCvrIw5xDaZKeDuRmNjwJLWgswRU5MMKRZM1HgQDpTDg0mmaXvNzfabMPCR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