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

allow dynamic locals #1594

Merged
merged 1 commit into from
May 31, 2019
Merged
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
11 changes: 6 additions & 5 deletions lib/src/rules/omit_local_variable_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ class _Visitor extends SimpleAstVisitor<void> {
if (loopParts is ForPartsWithDeclarations) {
_visitVariableDeclarationList(loopParts.variables);
} else if (loopParts is ForEachPartsWithDeclaration) {
final staticType = loopParts.loopVariable.type;
if (staticType == null) {
final loopVariableType = loopParts.loopVariable.type;
final staticType = loopVariableType?.type;
if (staticType == null || staticType.isDynamic) {
return;
}
final iterableType = loopParts.iterable.staticType;
Expand All @@ -82,8 +83,8 @@ class _Visitor extends SimpleAstVisitor<void> {
.where((type) =>
DartTypeUtilities.isInterface(type, 'Iterable', 'dart.core'));
if (iterableInterfaces.length == 1 &&
iterableInterfaces.first.typeArguments.first == staticType.type) {
rule.reportLint(staticType);
iterableInterfaces.first.typeArguments.first == staticType) {
rule.reportLint(loopVariableType);
}
}
}
Expand All @@ -96,7 +97,7 @@ class _Visitor extends SimpleAstVisitor<void> {

_visitVariableDeclarationList(VariableDeclarationList node) {
final staticType = node?.type?.type;
if (staticType == null) {
if (staticType == null || staticType.isDynamic) {
return;
}
for (final child in node.variables) {
Expand Down
14 changes: 14 additions & 0 deletions test/rules/omit_local_variable_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

// test w/ `pub run test -N omit_local_variable_types`

// ignore_for_file: prefer_foreach

f() {
dynamic x = 0; // OK
dynamic y = x; // OK
print(y);
}

abstract class StringIterator<E> implements Iterable<E> {}

void printItems2(StringIterator<String> items) {
Expand All @@ -12,6 +20,12 @@ void printItems2(StringIterator<String> items) {
}
}

void printItems2a(StringIterator<String> items) {
for (dynamic item in items) { // OK
print(item);
}
}

abstract class StringIterator2 implements StringIterator<String> {}

void printItems3(StringIterator2 items) {
Expand Down