Skip to content

identical_operands

Reports an operation whose two operands are the same value, such as a.id == a.id. Write the operand you meant, or drop the operation.

Tier
blocking
Scope
Dart
Lowest preset
recommended
Fix
none

An operation whose two operands are the same value, so its result is known before it runs, or is that value again. The violation covers the whole operation. The operations are:

  • a comparison, ==, !=, <, <=, > or >=: a.id == a.id is always true;
  • &&, || and ??: ready && ready is ready;
  • the arithmetic -, /, ~/, % and ^: width - width is 0;
  • identical(next, next).

Two operands are the same when they hold the same tokens in the same order and every name means the same declaration, as no_equal_branches compares branches. Whitespace and comments do not count. Another literal, a pair of parentheses or another quote makes them differ.

Only an operand that gives the same value each time is compared, such as a local variable, a parameter or a final field.

Which values count as the same twice
  • a literal, or a constant: a const collection or object, a value of an enum;
  • a type, this, a local variable or a parameter;
  • a field or a top-level variable that is final or const;
  • the core members that only read their object: length, isEmpty, isNotEmpty, first, last, isNaN, isNegative, isFinite, isInfinite, isEven, isOdd, sign, hashCode and runtimeType;
  • what is built from these with . and ?., the core library’s operators, is, as, !, a conditional expression and identical.

A getter written in the source, an index operator, a call, an assignment and a newly created collection or object are never compared: each can give another value the second time.

It does not report:

  • two literals, 1 == 1: literal_only_boolean_expressions reports the boolean ones;
  • ==, !=, <= and >= on a value that can be a double, a num, an Object, a dynamic value or a type parameter whose bound allows a double: there x == x is the test for NaN;
  • == and != of a type that declares its own ==: a test of that operator compares a value with itself on purpose;
  • an arithmetic or relational operator that a type of the source declares, whose result nothing tells;
  • x * x, x + x, x & x and x | x, whose result is not fixed.

It is almost always a copy whose second half was not changed: a.id == a.id where a.id == b.id was meant, end - end where end - start was. Every pair of objects is then equal, and every difference zero. AI coding agents write this when they complete a comparison from the left operand. The code compiles and reads naturally, and a test with one object passes.

The rule is blocking because the operation never does what it shows.

Violating
bool sameItem(Item a, Item b) => a.id == a.id;
Duration elapsed(DateTime start, DateTime end) => end.difference(end);
int remaining(int total, int used) => total - total;
Corrected
bool sameItem(Item a, Item b) => a.id == b.id;
int remaining(int total, int used) => total - used;

The call end.difference(end) is not an operator and is not reported.

The rule has only the two common rule parameters, set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.
assay:
rules:
identical_operands:
exclude: ["lib/generated/**"]

Write why on the line directly above the suppression, which goes above the line of the left operand, or of identical:

// The value is compared with itself to show that it is not NaN.
// ignore: identical_operands
if (ratio == ratio) {
show(ratio);
}

// ignore_for_file: assay/identical_operands silences it in a whole file, and // ignore: assay/identical_operands with the prefix works too. See Suppressions.