V7007. The '|' and '&' operators bypass short-circuit evaluation. The right-hand operand will be evaluated even when the left operand is false.
The analyzer has detected the use of an operator without short-circuit evaluation (& or |) when it may be intended to use a short-circuit operator (&& or ||).
The example:
function process(obj) {
if (obj !== null & isValid(obj)) {
// ....
}
}
function isValid(obj) {
return obj.someProperty !== undefined
}
If resource-intensive operations are used as operands, the use of bitwise operations is inefficient. In addition, such code may lead to errors because operand types and operation precedence may be different. It may also result in unexpected behavior if the right-hand operand has side effects or depends on conditions set by the left-hand operand.
For example, if null is passed as the first argument to the process function, the right-hand operand is evaluated despite the check, resulting in an exception:
TypeError: Cannot read properties of null (reading 'someProperty')
The fixed code:
function process(obj) {
if (obj !== null && isValid(obj)) {
// ....
}
}
function isValid(obj) {
return obj.someProperty !== undefined
}