Skip to content
NewBun 1.4.5 is out. Release notes
AphrodyBun

Linter · Rules

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

Reported:

if (user.active & user.verified) {
  grant(user);
}

Not reported:

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:

const mode = READ | WRITE;
const canWrite = (mode & WRITE) !== 0;

With int32Hint, neither is this:

const whole = price | 0;

Any other number is still reported:

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.