avoid-inferrable-type-arguments
preset: recommended
Warns when an inferrable type argument can be removed without affecting the code.
This rule mostly covers return statements, method invocations and collection literals.
warning
This rule is not compatible with prefer-explicit-type-arguments
.
⚙️ Config
Set ignored-invocations
(default is empty) to ignore type arguments of a specific invocation.
dart_code_metrics:
...
rules:
...
- avoid-inferrable-type-arguments:
ignored-invocations:
- context.read
- GetIt.I
- watch
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];