no-constant-zero-expression
UnreleasedConfiguration
Rule Details
Disallow multiplication or bitwise AND with the numeric literal 0, and
division with 0 as the numerator. These operations often indicate a typo
or leftover debugging code. Other zero literal forms, such as 0.0 and
0x0, are also checked.
Examples of incorrect code:
Examples of correct code:
Suggestions
The rule suggests replacing an expression with 0 only when it can safely
evaluate the whole expression to positive zero and the expression contains no
comments. Negative zero, NaN, unknown values, side effects and mutable
member reads prevent the suggestion. The rule does not apply automatic fixes.
Expressions such as x * -0, x * 0n, and x * (0 as number) are ignored.
Division by zero (x / 0) and other operators are outside this rule's scope.
Options
This rule has no options.
Differences from upstream
Suggestions preserve spacing next to keywords and TypeScript operators. For
example, return(5)*0 becomes return 0, and 5*(0)as number becomes
0 as number.
rslint withholds a suggestion when it cannot establish the value of an operand
discarded by a comma expression or void. For example, (1n / 0n, 5) * 0
and (void missing) & 0 are reported without a suggestion, preserving their
RangeError and ReferenceError. Known operands such as (1, 5) * 0 and
(void 5) & 0 still receive suggestions.
Deeply shared constant dependencies can also receive a diagnostic without a suggestion. For example, a long series of constants that each add the previous constant to itself is not fully expanded just to produce an editor suggestion.
Built-in function values are reported without suggestions. For example,
String.raw & 0, 0 & String.raw, and
(typeof String.raw === "function" ? 5 : NaN) * 0 receive diagnostics without
the replacement suggested upstream. This remains conservative when the function
has been modified, such as by assigning String.raw[Symbol.toPrimitive].