noSubtractionComparison (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Rule available since:
v2.6.6 - Diagnostic Category:
lint/nursery/noSubtractionComparison - This rule has an unsafe fix.
- The default severity of this rule is warning.
- Sources:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noSubtractionComparison": "error" } } }}Description
Section titled “Description”Disallow subtracting two values only to compare the result with 0.
Code such as a - b > 0 subtracts one value from another, then checks whether the
difference is above, below, or equal to 0. Comparing the two values directly, as in
a > b, is shorter and states the intent more clearly.
This pattern often remains after code is copied from a sort comparison function such as
(a, b) => a - b.
The rule checks the operators >, >=, <, <=, ===, !==, ==, and !=, with
0 on either side. When 0 is on the left, the fix flips the operator, so 0 < a - b
becomes a > b.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”if (a - b > 0) {}code-block.js:1:5 lint/nursery/noSubtractionComparison FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This comparison subtracts two values and compares the difference with 0.
> 1 │ if (a - b > 0) {}
│ ^^^^^^^^^
2 │
ℹ Compare the two values directly instead, which is shorter and states the intent more clearly.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Compare the values directly.
1 │ - if·(a·-·b·>·0)·{}
1 │ + if·(a·>·b)·{}
2 2 │
if (a - b <= 0) {}code-block.js:1:5 lint/nursery/noSubtractionComparison FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This comparison subtracts two values and compares the difference with 0.
> 1 │ if (a - b <= 0) {}
│ ^^^^^^^^^^
2 │
ℹ Compare the two values directly instead, which is shorter and states the intent more clearly.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Compare the values directly.
1 │ - if·(a·-·b·<=·0)·{}
1 │ + if·(a·<=·b)·{}
2 2 │
if (0 < a - b) {}code-block.js:1:5 lint/nursery/noSubtractionComparison FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This comparison subtracts two values and compares the difference with 0.
> 1 │ if (0 < a - b) {}
│ ^^^^^^^^^
2 │
ℹ Compare the two values directly instead, which is shorter and states the intent more clearly.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Compare the values directly.
1 │ - if·(0·<·a·-·b)·{}
1 │ + if·(a·>·b)·{}
2 2 │
if (a - b === 0) {}code-block.js:1:5 lint/nursery/noSubtractionComparison FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This comparison subtracts two values and compares the difference with 0.
> 1 │ if (a - b === 0) {}
│ ^^^^^^^^^^^
2 │
ℹ Compare the two values directly instead, which is shorter and states the intent more clearly.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Compare the values directly.
1 │ - if·(a·-·b·===·0)·{}
1 │ + if·(a·===·b)·{}
2 2 │
if (a > b) {}if (a - b > 1) {}if (a + b > 0) {}if (a - b > 0n) {}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.