Skip to main content

avoid-passing-self-as-argument

added in: 1.9.0
Pro+
preset: recommended

Warns when an object is used as an argument to its own method.

Example

❌ Bad:

void main() {
final list = [1, 2, 3];

list.addAll(list); // LINT: This object is used as an argument to its own method. Try using a different argument or rewriting the method implementation.

final object = Some();
object.work(object); // LINT: This object is used as an argument to its own method. Try using a different argument or rewriting the method implementation.

final another = Another([1, 2, 3]);
another.values.addAll(another.values); // LINT: This object is used as an argument to its own method. Try using a different argument or rewriting the method implementation.
}

class Some {
void work(Some another) {}
}

class Another {
final List<int> values;

const Another(this.values);
}

✅ Good:

void main() {
final list = [1, 2, 3];
final anotherList = [4, 5, 6];

list.addAll(anotherList); // Correct, another argument

final another = Another([1, 2, 3]);
another.values.addAll(anotherList);
}

Additional Resources