Rules / Suspicious / no-self-comparisons

no-self-comparisons

lint/suspicious/no-self-comparisons
recommended
Same as no_self_comparisons · Pyramid Lint

Flags a comparison whose two operands are identical.

An expression like x == x or count < count compares a value with itself, so ==, <=, and >= are always true while !=, <, and > are always false. It is almost always a typo for a different variable or a leftover from a refactor, and the constant result masks the check that was intended. Operands are compared by source text with whitespace removed, covering ==, !=, <, >, <=, and >=. Fix the operand that was meant to differ.

Invalid

example.dartdart
bool a(int x) => x == x;

bool b(int x) => x != x;

bool c(int x) => x < x;

class Foo {
  int value = 0;

  bool check() => value >= value;

  bool nested(List<int> a) => a[0] == a[0];
}

void d(int x) {
  if (x > x) {
    print('never');
  }
}
Both operands of this comparison are identical.
1bool a(int x) => x == x;
Both operands of this comparison are identical.
2
3bool b(int x) => x != x;
Both operands of this comparison are identical.
4
5bool c(int x) => x < x;
Both operands of this comparison are identical.
9
10 bool check() => value >= value;
Both operands of this comparison are identical.
11
12 bool nested(List<int> a) => a[0] == a[0];
Both operands of this comparison are identical.
15void d(int x) {
16 if (x > x) {

Valid

example.dartdart
bool a(int x, int y) => x == y;

bool b(int x) => x == x + 1;

bool c(int x) => x < x + 1;

class Foo {
  int a = 0;
  int b = 0;

  bool check() => a == b;

  bool indexed(List<int> l) => l[0] == l[1];
}

void d(int x, int y) {
  if (x > y) {
    print('maybe');
  }
}

bool e(int x) => -x == x;

How to configure

Set the severity of no-self-comparisons in your falcon.json:

falcon.jsonjson
{
  "linter": {
    "rules": {
      "suspicious": {
        "no-self-comparisons": "error"
      }
    }
  }
}