no-self-compare
Comparing a variable to itself is almost always a mistake, either a typo or refactoring error.Rule Type: Problem
Fixable: No
Fixable: No
Why This Rule Exists
There are almost no situations where you would want to compare something to itself. The only common case is testing forNaN, but there are better ways to do that.
Rule Details
This rule flags any comparison where both sides are syntactically identical.Examples
Incorrect Code
Correct Code
Common Mistakes
Testing for NaN (Wrong)
Copy-Paste Errors
Refactoring Errors
Known Limitations
This rule uses simple syntactic comparison and may produce false positives in rare cases:Better Alternatives
NaN Detection
Value Validation
When Not to Use It
This rule has very few legitimate exceptions. Only disable it if:- You have complex expressions where static analysis can’t determine they’re actually different
- You’re using a code generation tool that produces self-comparisons
Number.isNaN() instead of disabling the rule.