<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/89367>89367</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[BUG] Clang-tidy: False positive when using `hicpp-signed-bitwise.IgnorePositiveIntegerLiterals`
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang-tidy
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
sdimovv
</td>
</tr>
</table>
<pre>
Consider the following code:
```c
#include "stdint.h"
#include "stdio.h"
#include "inttypes.h"
int main(void) {
uint32_t u32val = 0;
printf("%"PRIu32, u32val & 1);
printf("%"PRIu32, u32val << 2);
return 0;
}
```
And the following config:
```yaml
# .clang-tidy
---
FormatStyle: file
WarningsAsErrors: '*'
Checks: >
-*,
hicpp-signed-bitwise,
CheckOptions:
hicpp-signed-bitwise.IgnorePositiveIntegerLiterals: true
```
When `clang-tidy` is ran this is the result:
```
test.c:8:32: error: use of a signed integer operand with a binary bitwise operator [hicpp-signed-bitwise,-warnings-as-errors]
8 | printf("%"PRIu32, u32val & 1);
| ~ ^
```
The docs for the `IgnorePositiveIntegerLiterals` option state:
> If this option is set to true, the check will not warn on bitwise operations with positive integer literals.
What I believe is happening here is that implicit conversions are not skipped for the RHS operand, thus when performing the `&` op, the literal `1` is implicitly cast from an `int` to an `unsigned int`, and is no longer considered a literal.
But the same thing doesn't happen for the `<<` operation, because for that operation the operands on both sides do not have to be the same type, thus the literal `2` is not implicitly casted.
Note that implicit casts are skipped for the LHS operand. This can be observed with the following code:
```c
#include "stdint.h"
#include "stdio.h"
#include "inttypes.h"
int main(void) {
uint8_t u8val = 0;
printf("%"PRIu8, u8val & 1);
printf("%"PRIu8, u8val << 2);
return 0;
}
```
The above code raises no errors when checked with `clang-tidy`, even though `u8val` is implicitly casted to an `int` for both the `&` and `<<` operations.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUVk2P2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