A shift count is at least the number of bits of the shifted value

tx0/csharp:shift-rangedefaultwarningreliability/arithmeticdepth fastbug

What it finds

C# masks shift counts: an int shifted by 32 is shifted by 0, a long by 64 likewise. So 1 << 32 is 1, not 4294967296. A count of 64 or more is wrong for every type; 32 to 63 is wrong when the shifted value is an int or uint literal or variable.

From the rule's source, shift_range.rs.

Example from the rule's tests
class Bits{    long Run(int x, long y)    {        var a = 1 << 32;tx0 reports: shifting by 32 shifts by 0 because the count is masked to the width of the value        var b = x >> 40;tx0 reports: shifting by 40 shifts by 8 because the count is masked to the width of the value        var c = y << 40;        var d = y << 64;tx0 reports: shifting by 64 shifts by 0 because the count is masked to the width of the value        return a + b + c + d + (1L << 40);    }}
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 in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/csharp:shift-range" = "off"

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/csharp:shift-range] reason="..."

tx0 explain tx0/csharp:shift-range shows the same in the terminal. Found a false positive? Report it.