Skip to main content

avoid-inferrable-type-arguments

added in: 1.11.0
🛠

Warns when an inferrable type argument can be removed without affecting the code.

This rule mostly covers return statements, method invocations and collection literals.

danger

This rule is not compatible with prefer-explicit-type-arguments.

Example

❌ Bad:

void main() {
withList(<num>[1]); // LINT

final List<int>? nullableList = null;
withList(nullableList ?? <num>[1]); // LINT
withList(nullableList == null ? <num>[1] : <num>[2]); // LINT

withGenericList(<String>['1']); // LINT

withList(withGenericList<num>([1])); // LINT
withList(onlyParam<String>('1')); // LINT
}

void withList(List<num> items) {}

List<T> withGenericList<T>(List<T> items) => items;

List<num> onlyParam<P>(P param) => [1];

✅ Good:

void main() {
withList(<int>[1]);
withList([1]);

final List<int>? nullableList = null;
withList(nullableList ?? [1]);
withList(nullableList == null ? [1] : [2]);

withGenericList(['1']);

withList(withGenericList([1]));
withList(onlyParam('1'));
}

void withList(List<num> items) {}

List<T> withGenericList<T>(List<T> items) => items;

List<num> onlyParam<P>(P param) => [1];