no-bitwise

Disallow bitwise operators

Most application code never needs &, |, ^, ~, <<, >> or >>>, so a single & or | is usually a typo for && or ||. The code still runs, but it evaluates both sides every time and gives a number instead of one of the operands.

The assignment forms such as |= and <<= are reported too.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"no-bitwise" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"no-bitwise" = ["error", {
  allow = [], # list of strings
  int32Hint = false,
}]

Examples#

example.js1 problem
if (user.active & user.verified) {
  grant(user);
}
example.jsNo problems
if (user.active && user.verified) {
  grant(user);
}

Options#

bunfig.toml
[lint.rules]
no-bitwise = ["error", { allow = ["&", "|"] }]
OptionDefaultWhat it does
allow[]Operators that aren't reported.
int32HintfalseAllow value | 0, a way to cut a number down to a 32-bit integer.

With allow: ["&", "|"], this isn't reported:

example.jswith { "allow": ["&", "|"] }No problems
const mode = READ | WRITE;
const canWrite = (mode & WRITE) !== 0;

With int32Hint, neither is this:

example.jswith { "int32Hint": true }No problems
const whole = price | 0;

Any other number is still reported:

example.jswith { "int32Hint": true }1 problem
const odd = price | 1;

When not to use it#

Code that works with flags, binary formats, hashes or colors needs these operators. Turn the rule off for those files, or list the operators you use in allow.