A negative value is shifted left

tx0/c:negative-left-shiftstrictwarningbug

What it finds

-1 << n: shifting a negative value left is undefined behavior in C before C23. ~0u << n, or the mask computed in an unsigned type, is defined; a literal with the suffix u is unsigned already and left alone. Compilers in practice give -1 << n the value two's complement suggests, and much code relies on that, so this is a strict rule.

From the rule's source, negative_left_shift.rs.

Example from the rule's tests
int mask(int n){  return (-1 << n) | (~0u << n);tx0 reports: the negative value -1 is shifted left}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/c:negative-left-shift" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/c:negative-left-shift] reason="..."

tx0 explain tx0/c:negative-left-shift shows the same in the terminal. Found a false positive? Report it.