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

    <tr>
        <th>Summary</th>
        <td>
            false positive in bugprone-too-small-loop-variable checker when using bitfield as upper bound
        </td>
    </tr>

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

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

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

<pre>
    The bugprone-too-small-loop-variable checker does not consider bitfields, not to ignore or flag them as poor programming practice (if that's desired) or consider their bitwidth, leading to false positives when this kind of code is analyzed. An example is below. 

If I cast the upperbound in this case there's no warning, or if I assign the upper bound to an integral variable of equivalent or lower rank than the loop variable, then there's no warning either. So these seem to be suitable workarounds. 

I suppose the checker could get the real bitfield width and use that
1) const FieldDecl *FD = UpperBound->getSourceBitField()
2) if FD; FD->getBitWidthValue(Context)
or maybe just detect if the loop variable and/or upper bounds are a bitfield and skip the check
1) const FieldDecl *FD = UpperBound->getSourceBitField()
2) if FD; FD->isBitField()

struct xx {
    int x;
    int y:6;
} xx;

int foo(struct xx *pxx) {
    int sum = 0;
    for (char yy=0; yy<pxx->y; yy++)
        sum++;
    return sum;
}

clang-tidy --checks=bugprone-too-small-loop-variable loop.c --

loop.c:9:21: warning: loop variable has narrower type 'char' than iteration's upper bound 'int' [bugprone-too-small-loop-variable]
    for (char yy=0; yy<pxx->y; yy++)

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy9Vcuu4jgQ_ZqwKYFCwnORBVwaqdc9j7UTVxI3Jk7bDlzm6-fYuc2je6QeaaThYWK76tSpU3ZRGnkrfmuZyqHprel46o2ZurPQeqqN6acXYZUoNVPVcnViS9Kwo854qkznlMRKqXytWEuXZG9xxxtSTWcsk7FUa9GQb_lMwlFvsII4jRXns-oaPIvKq4opyTaqhp3wSbZ2JNkpyzLJtgHjHgo4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