Skip to content

receiver_passed_to_itself

Reports a collection or string passed to one of its own methods that cannot mean it, such as items.addAll(items). Pass the value you meant.

Tier
blocking
Scope
Dart
Lowest preset
recommended
Fix
none

A call whose outcome is fixed when the receiver is its own argument. The message says which outcome it is:

  • It throws: addAll of a List or Queue, and removeAll of a Set, change the collection while they read it and throw a ConcurrentModificationError (a DoubleLinkedQueue never ends).
  • It changes nothing: addAll of a Set or Map, retainAll of a Set.
  • It always gives the same answer: containsAll of a Set (always true), difference (always empty), and contains, startsWith, endsWith of a String (always true).

Only the SDK’s members are judged, not an override in the project. The receiver and the argument must be the same code reading the same object: a local, a parameter, this or a field, alone or at the end of a chain of them. A getter written in the source, or a call, may give another object. The violation covers the call.

It does not report:

  • setRange, setAll, replaceRange, followedBy, insertAll, whose result depends on the index;
  • compareTo;
  • ==, which identical_operands reports;
  • contains of a collection given itself, which the SDK lint collection_methods_unrelated_type reports where the types are unrelated.

Each of these calls has a fixed outcome no one writes it for: a crash, nothing, or a constant answer. It is almost always the wrong variable (items.addAll(items) for items.addAll(more)), or a copy that was meant (items.addAll([...items])).

Violating
void restore(Set<String> selected, Set<String> saved) {
selected.removeAll(selected);
selected.addAll(saved);
}
Corrected
void restore(Set<String> selected, Set<String> saved) {
selected
..clear()
..addAll(saved);
}

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.

Write why on the line directly above the suppression:

// Why it stays as written here.
// ignore: receiver_passed_to_itself

// ignore_for_file: assay/receiver_passed_to_itself silences it in a whole file, and the prefixed // ignore: assay/receiver_passed_to_itself works too. See Suppressions.