Skip to content

unnecessaryComparisons

Reports comparisons that are always true, always false, or can be simplified.

✅ This rule is included in the ts logical and logicalStrict presets.

This rule detects comparison patterns that are logically unnecessary, impossible, or can be simplified. Such patterns often indicate logic errors, copy-paste mistakes, or overly complex code that should be refactored.

The rule covers several categories of problematic comparisons:

  1. Self-comparisons: Comparing a value to itself (e.g., x === x)
  2. Impossible ranges: Range checks that can never be satisfied (e.g., x <= 400 && x > 500)
  3. Redundant double comparisons: OR patterns that can be simplified (e.g., x === y || x < y can become x <= y)
  4. Ineffective checks: Redundant bounds in AND chains (e.g., x < 200 && x <= 299 where the second check is always true when the first is)
if (value === value) {
// Always true
}
if (value < value) {
// Always false
}
// Can never be true: no value is both <= 400 AND > 500
if (x <= 400 && x > 500) {
// Unreachable code
}
// Impossible boundary condition
if (x < 5 && x >= 5) {
// Never executes
}
// Can be simplified to: x <= y
if (x === y || x < y) {
// ...
}
// Can be simplified to: x >= y
if (x === y || x > y) {
// ...
}
// x <= 299 is redundant when x < 200 is already checked
if (x < 200 && x <= 299) {
// The second check adds nothing
}
// x >= 50 is redundant when x > 100 is already checked
if (x > 100 && x >= 50) {
// ...
}

This rule is not configurable.

If your codebase relies on dynamic getters or other side effects that could make seemingly-identical expressions return different values, you might want to disable this rule for those specific cases.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.