A constant int is shifted left into or past its sign bit
What it finds
1 << 31: the literal is a signed int, and shifting a one into its sign bit is undefined behavior before C23 and gives a negative number that turns into 0xffffffff80000000 when widened. 1u << 31 is what such code means.
The left operand must be an int literal without suffix, the count a constant less than 32; larger counts are left to shift-range.
From the rule's source, shift_into_sign_bit.rs.
unsigned int masks(void){ unsigned int top = 1 << 31;tx0 reports: 1 << 31 shifts into the sign bit of an int unsigned int ok = 1u << 31; unsigned int low = 1 << 30; return top | ok | low | (3 << 30);tx0 reports: 3 << 30 shifts into the sign bit of an int} 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:
[rules]
"tx0/c:shift-into-sign-bit" = "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:shift-into-sign-bit] reason="..."tx0 explain tx0/c:shift-into-sign-bit shows the same in the terminal. Found a false positive? Report it.