For AI agents: the complete documentation index is available at https://rslint.rs/llms.txt, the full documentation bundle is available at https://rslint.rs/llms-full.txt, and this page is available as Markdown at https://rslint.rs/rules/unicorn/no-constant-zero-expression.md.
close

no-constant-zero-expression

Unreleased

Configuration

rslint.config.ts
import { defineConfig, unicornPlugin } from '@rslint/core';

export default defineConfig([
  unicornPlugin.configs.recommended,
  {
    rules: {
      'unicorn/no-constant-zero-expression': 'error',
    },
  },
]);

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:

const total = price * 0;
const flags = mask & 0;
const ratio = 0 / count;

Examples of correct code:

const total = 0;
const flags = 0;
const ratio = 0;

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].

Original Documentation