no-constant-comparison
C / C++C / C++C#JavaJavaScript / TypeScriptKotlinPHPPythonRubysuspiciousno fixdefault: warnjs/eslint-recommendedjs/oxlint
What it does
Section titled “What it does”Reports an equality or a comparison whose two sides are both literals (1 === 2, "a" < "b"). An operand that is an expression of literals (1 + 1 === 2) is not reported.
Why it matters
Section titled “Why it matters”The result of a comparison of two literals is fixed when the code is written, so one branch of the if never runs. It is usually a leftover of debugging, or of a value that was meant to be a variable.
To fix, compare the variable, or replace the test with its result.
Example
Section titled “Example”...if (1 === 2) {} if (a === 1) {}...Configuration
Section titled “Configuration”c: lint: rules: no-constant-comparison: warn # off, warn (default), errorcpp: lint: rules: no-constant-comparison: warn # off, warn (default), errorcsharp: lint: rules: no-constant-comparison: warn # off, warn (default), errorjava: lint: rules: no-constant-comparison: warn # off, warn (default), errorjs: lint: rules: no-constant-comparison: warn # off, warn (default), errorkotlin: lint: rules: no-constant-comparison: warn # off, warn (default), errorphp: lint: rules: no-constant-comparison: warn # off, warn (default), errorpython: lint: rules: no-constant-comparison: warn # off, warn (default), errorruby: lint: rules: no-constant-comparison: warn # off, warn (default), errorEnabled by the presets: js/eslint-recommended, js/oxlint.
Suppress one line with sudsy-disable-next-line no-constant-comparison in a comment of the file’s language.
Upstream equivalents
Section titled “Upstream equivalents”The same check in other tools. full: Sudsy reports what the upstream rule reports; partial: only part of it, as the last column says.
| Tool | Rule | Match | What differs |
|---|---|---|---|
| eslint | no-constant-binary-expression |
partial | only two literals compared |