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

    <tr>
        <th>Summary</th>
        <td>
            clang-tidy UndefinedBinaryOperatorResult possible false positive for shift of 0
        </td>
    </tr>

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

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

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

<pre>
    With clang-tidy 14.0.5, compiling this test program produces a warning for shift of 0:

```
#include <cstdint>

static constexpr uint32_t get_shifted(uint32_t v)
{
    return v << 6;
}

extern struct _bitf
{
    uint64_t mode : 1;
} bitf;

static void func()
{
    const bool bitf_mode = bitf.mode;

    bool other_mode = true;

    const bool not_used = bitf_mode && !other_mode;

    uint32_t val = get_shifted((uint32_t)bitf_mode);
}
```

Compiling with `clang-tidy --checks=-*,clang-analyzer-core.UndefinedBinaryOperatorResult bug.cpp --  -std=c++17` results in:

```
/tmp/bug.cpp:5:14: warning: The result of the left shift is undefined due to shifting '0' by '6', which is unrepresentable in the unsigned version of the return type 'uint32_t' [clang-analyzer-core.UndefinedBinaryOperatorResult]
    return v << 6;
             ^
/tmp/bug.cpp:15:5: note: 'bitf_mode' initialized here
    const bool bitf_mode = bitf.mode;
    ^~~~~~~~~~~~~~~~~~~~
/tmp/bug.cpp:19:27: note: Assuming 'bitf_mode' is false
    const bool not_used = bitf_mode && !other_mode;
                          ^~~~~~~~~
/tmp/bug.cpp:19:37: note: Left side of '&&' is false
    const bool not_used = bitf_mode && !other_mode;
                                    ^
/tmp/bug.cpp:21:32: note: Passing value via 1st parameter 'v'
    uint32_t val = get_shifted((uint32_t)bitf_mode);
                               ^~~~~~~~~~~~~~~~~~~
/tmp/bug.cpp:21:20: note: Calling 'get_shifted'
    uint32_t val = get_shifted((uint32_t)bitf_mode);
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/tmp/bug.cpp:5:14: note: The result of the left shift is undefined due to shifting '0' by '6', which is unrepresentable in the unsigned version of the return type 'uint32_t'
    return v << 6;
           ~ ^
```

Perhaps I'm missing something, but the shift seems valid.  The same code doesn't produce any warnings in version 13.0.  Also, for what it's worth if I change the code so that bitf_mode comes from an extern bool rather than the struct member, I don't see the warning anymore.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzNVt9v4zYM_mucFyKBLSdx85CHJL0CBQbsMGzYYyHbdKxNlgxJTi7314-SncTt0q7Xuw0zFP-IaZIf-VFkrsvT-nfhaigkV_upE-UJkvksni0itoNCN62QQu3B1cKCQ-ugNXpveOOvZVegBQ5HbpQXqrQBW4vKga4gjtJNFN9H8fm8jIfVP7JUqEJ2JUKU7grrSqFclH4af2Idd6IgL5R1-KU10JFMyp4c7NE9BUtYRuzu8vchYqtBQbbtb4AOg64zCg7eEi1YRun2LHY_NohfHJKgdaYrHDzlwlU31HlzyzmZa3TwfgPJWCGEzy5_jJEctCih6lRBTt92NWCFXGsZ1DwNJnqlM__0QrP_KIhrV6O5yhOEW6Ij_Uq7p85ieVE_fMyWtOiSXDXeUHSNOZdBw_OUjLJCQC_aPei_x_4FM8J5d6He0dOT3o4YOp0WNRZ_WrI7jRhxade_5IrL01c000IbnP2mSqyEwnIrFDenn1s03GnzC9pOUgy6_axoW9IFMCX6kS5Ky5ZWkpE1Io0XsyDUPxH5wTUtnQeFJL2gXzL3xBgqw9_-WuOg01cHhRYkUqX09ULF1Z29hbJDcLp_4wMQsSymH-Qnf0vZyXxtHmtR1P2HBlvSjMrxXCI5HLR3yoq9V3dAY4VWZ6tDMbhT63OdXbOUQbTYfnMco8X9ewoNxke0-PRq7JLFEEDPT_RX8mzEn4zwCSe4FF8JGxEUP1Q8Zzeyj65X_SeCbyhDIwAba7tmSORzJBYqLu1tBB8pT3j1eAvrm1DSZ1B-CpQV5AexKRBxGdZ_iuV9RGKJ956Nvf_MrfV5oC2LSuwgO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