Issue 240838
Summary [clang-tidy] Enabling IgnorePositiveIntegerLiterals misses genuine issues
Labels clang-tidy
Assignees
Reporter vit9696
    `hicpp-signed-bitwise` rule is meant to protect the programmer from misusing bitwise arithmetics with signed integers.

Sometimes the rule is too strong, for example the following code snippet is obviously safe but will produce a warning:

```c
uint32_t value = <...>;
value >>= 4;
```

To overcome that, `IgnorePositiveIntegerLiterals` option was introduced in https://github.com/llvm/llvm-project/commit/4de6b1586807285e20a5db6596519c2336a64568.

This option, however, is implemented incorrectly as it skips several genuine scenarios, which are clear mistakes. For example, the following code snippet correctly produces a warning with `IgnorePositiveIntegerLiterals` disabled but incorrectly produces no warning when `IgnorePositiveIntegerLiterals` is enabled:

```
uint8_t x = 2;
printf("Resulting value is: %u\n", (1 << 30U) * x);
```

In C this bitshift evaluates to `signed int`, which then results in a signed integer overflow upon multiplication.

To me the correct relaxation of the `hicpp-signed-bitwise` rule is not to completely ignore all integer literals in the _expression_ the way `IgnorePositiveIntegerLiterals` currently does, but to ignore the literal argument _irrelevant_ to integer promotion. I.e.

```c
1 << 30 // should warn
1U << 30 // should not warn
1U << 30U // should not warn
```

For backwards compatibility reasons `IgnorePositiveIntegerLiterals` option could be transformed to an enum, so that:

- the old behaviour (`true`) will be something like `All` 
- the new behaviour (described in the code snippet above) will be something like `OnlyIrrelevant`

сс @VladimirPlyashkun, @AaronBallman, @JonasToth
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to