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
What it reports
Section titled “What it reports”A call whose outcome is fixed when the receiver is its own argument. The message says which outcome it is:
- It throws:
addAllof aListorQueue, andremoveAllof aSet, change the collection while they read it and throw aConcurrentModificationError(aDoubleLinkedQueuenever ends). - It changes nothing:
addAllof aSetorMap,retainAllof aSet. - It always gives the same answer:
containsAllof aSet(always true),difference(always empty), andcontains,startsWith,endsWithof aString(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;==, whichidentical_operandsreports;containsof a collection given itself, which the SDK lintcollection_methods_unrelated_typereports 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])).
Example
Section titled “Example”void restore(Set<String> selected, Set<String> saved) { selected.removeAll(selected); selected.addAll(saved);}void restore(Set<String> selected, Set<String> saved) { selected ..clear() ..addAll(saved);}Rule parameters
Section titled “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. |
Suppressing it
Section titled “Suppressing it”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.