@@ -6,9 +6,9 @@ import 'package:analyzer/dart/ast/ast.dart';
6
6
import 'package:analyzer/dart/ast/visitor.dart' ;
7
7
import 'package:analyzer/dart/element/element.dart' ;
8
8
import 'package:analyzer/dart/element/type.dart' ;
9
+ import 'package:analyzer/dart/element/type_provider.dart' ;
9
10
10
11
import '../analyzer.dart' ;
11
- import '../extensions.dart' ;
12
12
import '../linter_lint_codes.dart' ;
13
13
14
14
const _desc = r'Omit type annotations for local variables.' ;
@@ -84,7 +84,7 @@ class OmitLocalVariableTypes extends LintRule {
84
84
@override
85
85
void registerNodeProcessors (
86
86
NodeLintRegistry registry, LinterContext context) {
87
- var visitor = _Visitor (this );
87
+ var visitor = _Visitor (this , context.typeProvider );
88
88
registry.addForStatement (this , visitor);
89
89
registry.addVariableDeclarationStatement (this , visitor);
90
90
}
@@ -93,7 +93,9 @@ class OmitLocalVariableTypes extends LintRule {
93
93
class _Visitor extends SimpleAstVisitor <void > {
94
94
final LintRule rule;
95
95
96
- _Visitor (this .rule);
96
+ final TypeProvider typeProvider;
97
+
98
+ _Visitor (this .rule, this .typeProvider);
97
99
98
100
@override
99
101
void visitForStatement (ForStatement node) {
@@ -103,18 +105,16 @@ class _Visitor extends SimpleAstVisitor<void> {
103
105
} else if (loopParts is ForEachPartsWithDeclaration ) {
104
106
var loopVariableType = loopParts.loopVariable.type;
105
107
var staticType = loopVariableType? .type;
106
- if (staticType == null || staticType is DynamicType ) {
107
- return ;
108
- }
109
- var iterableType = loopParts.iterable.staticType;
110
- if (iterableType is InterfaceType ) {
111
- // TODO(srawlins): Is `DartType.asInstanceOf` the more correct API here?
112
- var iterableInterfaces = iterableType.implementedInterfaces
113
- .where ((type) => type.isDartCoreIterable);
114
- if (iterableInterfaces.length == 1 &&
115
- iterableInterfaces.first.typeArguments.first == staticType) {
116
- rule.reportLint (loopVariableType);
117
- }
108
+ if (staticType == null || staticType is DynamicType ) return ;
109
+
110
+ var loopType = loopParts.iterable.staticType;
111
+ if (loopType is ! InterfaceType ) return ;
112
+
113
+ var iterableType = loopType.asInstanceOf (typeProvider.iterableElement);
114
+ if (iterableType == null ) return ;
115
+ if (iterableType.typeArguments.isNotEmpty &&
116
+ iterableType.typeArguments.first == staticType) {
117
+ rule.reportLint (loopVariableType);
118
118
}
119
119
}
120
120
}
@@ -124,20 +124,6 @@ class _Visitor extends SimpleAstVisitor<void> {
124
124
_visitVariableDeclarationList (node.variables);
125
125
}
126
126
127
- bool _dependsOnDeclaredTypeForInference (Expression ? initializer) {
128
- if (initializer is MethodInvocation ) {
129
- if (initializer.typeArguments == null ) {
130
- var element = initializer.methodName.staticElement;
131
- if (element is FunctionElement ) {
132
- if (element.returnType is TypeParameterType ) {
133
- return true ;
134
- }
135
- }
136
- }
137
- }
138
- return false ;
139
- }
140
-
141
127
void _visitVariableDeclarationList (VariableDeclarationList node) {
142
128
var staticType = node.type? .type;
143
129
if (staticType == null ||
@@ -147,13 +133,33 @@ class _Visitor extends SimpleAstVisitor<void> {
147
133
}
148
134
for (var child in node.variables) {
149
135
var initializer = child.initializer;
150
- if (initializer? .staticType != staticType) {
136
+ if (initializer == null || initializer .staticType != staticType) {
151
137
return ;
152
138
}
153
- if (_dependsOnDeclaredTypeForInference (initializer)) {
139
+
140
+ if (initializer is IntegerLiteral && ! staticType.isDartCoreInt) {
141
+ // Coerced int.
142
+ return ;
143
+ }
144
+
145
+ if (initializer.dependsOnDeclaredTypeForInference) {
154
146
return ;
155
147
}
156
148
}
157
149
rule.reportLint (node.type);
158
150
}
159
151
}
152
+
153
+ extension on Expression {
154
+ bool get dependsOnDeclaredTypeForInference {
155
+ if (this case MethodInvocation (: var methodName, typeArguments: null )) {
156
+ var element = methodName.staticElement;
157
+ if (element is FunctionElement ) {
158
+ if (element.returnType is TypeParameterType ) {
159
+ return true ;
160
+ }
161
+ }
162
+ }
163
+ return false ;
164
+ }
165
+ }
0 commit comments