Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

update always_specify_types to lint generic methods #1144

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/src/rules/always_specify_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class AlwaysSpecifyTypes extends LintRule implements NodeLintRule {
registry.addDeclaredIdentifier(this, visitor);
registry.addListLiteral(this, visitor);
registry.addMapLiteral(this, visitor);
registry.addMethodInvocation(this, visitor);
registry.addSimpleFormalParameter(this, visitor);
registry.addTypeName(this, visitor);
registry.addVariableDeclarationList(this, visitor);
Expand Down Expand Up @@ -164,4 +165,16 @@ class _Visitor extends SimpleAstVisitor<void> {
rule.reportLintForToken(list.keyword);
}
}

@override
void visitMethodInvocation(MethodInvocation node) {
if (node.typeArguments == null) {
final element = node.methodName.bestElement;
if (element is FunctionTypedElement &&
element.typeParameters.isNotEmpty &&
!element.metadata.any((a) => _isOptionalTypeArgs(a.element))) {
rule.reportLint(node.methodName);
}
}
}
}
14 changes: 14 additions & 0 deletions test/rules/always_specify_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,17 @@ void m() {
print("won't happen");
}
}

class A {
m1<T>() {}
@optionalTypeArgs
m2<T>() {}

m() {
A a;
a.m1(); // LINT
a.m1<int>(); // OK
a.m2(); // OK
a.m2<int>(); // OK
}
}