noAccidentalBitwiseOperators (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Rule available since:
v2.6.0 - Diagnostic Category:
lint/nursery/noAccidentalBitwiseOperators - This rule has an unsafe fix.
- The default severity of this rule is warning.
- Sources:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noAccidentalBitwiseOperators": "error" } } }}Description
Section titled “Description”Disallow bitwise operators where a logical operator was likely intended.
The bitwise operators &, |, and |= look a lot like the logical
operators &&, ||, and ||=, so it is easy to type one when you meant
the other. The two behave very differently:
- Bitwise operators always evaluate both sides. In
obj & obj.prop,obj.propis read even whenobjisnullorundefined, which throws an error. - Bitwise operators turn both sides into whole numbers. In
options | {}, the object becomes0, so the result is a number instead ofoptionsor{}.
This rule only reports cases where a logical operator was clearly intended:
&between a variable and a property of that same variable, such asobj & obj.prop.|or|=with a right side that can never be a useful number: an object, an array, a class, a function, a string, a template string, ortrue/false.
Bitwise math such as flags & MASK or value | 0 is not reported.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”if (obj & obj.prop) {}code-block.js:1:9 lint/nursery/noAccidentalBitwiseOperators FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ The bitwise operator & is used where the logical operator && was likely intended.
> 1 │ if (obj & obj.prop) {}
│ ^
2 │
ℹ Bitwise operators always evaluate both sides and turn them into numbers, so the result is not the value of either side.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Replace & with &&.
1 │ if·(obj·&&·obj.prop)·{}
│ +
options = options | {};code-block.js:1:19 lint/nursery/noAccidentalBitwiseOperators FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ The bitwise operator | is used where the logical operator || was likely intended.
> 1 │ options = options | {};
│ ^
2 │
ℹ Bitwise operators always evaluate both sides and turn them into numbers, so the result is not the value of either side.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Replace | with ||.
1 │ options·=·options·||·{};
│ +
input |= "";code-block.js:1:7 lint/nursery/noAccidentalBitwiseOperators FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ The bitwise operator |= is used where the logical operator ||= was likely intended.
> 1 │ input |= "";
│ ^^
2 │
ℹ Bitwise operators always evaluate both sides and turn them into numbers, so the result is not the value of either side.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Replace |= with ||=.
1 │ input·||=·"";
│ +
if (obj && obj.prop) {}options = options || {};input ||= "";const masked = flags & MASK;const truncated = value | 0;See Also
Section titled “See Also”- If you want to disallow bitwise operators entirely, see
noBitwiseOperators.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.