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-chained-comparison.md.
close

no-chained-comparison

Unreleased

Configuration

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

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

Rule Details

Disallow chained comparisons such as a < b < c. JavaScript evaluates this as (a < b) < c: the first comparison produces a boolean, which is then compared with c. Use && to compare each pair of operands.

Examples of incorrect code:

if (a < b < c) {}
if (a === b === c) {}

Examples of correct code:

if (a < b && b < c) {}
if (a === b && b === c) {}

// Intentional comparisons of boolean results are allowed.
if ((a > 0) === (b > 0)) {}
if ((a < b) === true) {}
if ((a > 0) !== Boolean(b)) {}

Suggestions

The rule offers an editor suggestion to split simple chains with &&. This changes the expression's behavior, so it is never applied as an automatic fix.

Suggestions are withheld when the middle operand has side effects, comments would be duplicated or removed, or the replacement would change grouping. Mixed ordering and equality operators, longer unparenthesized chains, and ordering comparisons with a boolean literal also do not receive a suggestion.

Options

This rule has no options.

Differences from upstream

For the following code, rslint reports the chained comparison without offering an editor suggestion. Unicorn v79.0.0 suggests a rewrite that can call tag twice and change its side effects:

a < tag`value` < c;

Evaluate the tagged template once, then write the intended comparisons:

const middle = tag`value`;
a < middle && middle < c;

rslint distinguishes local classes from browser built-ins with the same name. It reports the following comparison because the local Headers.has() returns a number. Unicorn v79.0.0 can accept it with TypeScript type information or a JSDoc annotation based on the class name:

export class Headers {
  has() {
    return 0;
  }
}

(a < b) === new Headers().has('x');

Original Documentation