From 5853b3ae6dd4dd5db477042168e2b9883f62d6a4 Mon Sep 17 00:00:00 2001 From: pq Date: Fri, 31 May 2019 15:34:54 -0700 Subject: [PATCH] allow dynamic locals --- lib/src/rules/omit_local_variable_types.dart | 11 ++++++----- test/rules/omit_local_variable_types.dart | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/src/rules/omit_local_variable_types.dart b/lib/src/rules/omit_local_variable_types.dart index 84c738afe..10dc7bf9d 100644 --- a/lib/src/rules/omit_local_variable_types.dart +++ b/lib/src/rules/omit_local_variable_types.dart @@ -71,8 +71,9 @@ class _Visitor extends SimpleAstVisitor { 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; @@ -82,8 +83,8 @@ class _Visitor extends SimpleAstVisitor { .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); } } } @@ -96,7 +97,7 @@ class _Visitor extends SimpleAstVisitor { _visitVariableDeclarationList(VariableDeclarationList node) { final staticType = node?.type?.type; - if (staticType == null) { + if (staticType == null || staticType.isDynamic) { return; } for (final child in node.variables) { diff --git a/test/rules/omit_local_variable_types.dart b/test/rules/omit_local_variable_types.dart index f4c7562ca..3cca36634 100644 --- a/test/rules/omit_local_variable_types.dart +++ b/test/rules/omit_local_variable_types.dart @@ -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 implements Iterable {} void printItems2(StringIterator items) { @@ -12,6 +20,12 @@ void printItems2(StringIterator items) { } } +void printItems2a(StringIterator items) { + for (dynamic item in items) { // OK + print(item); + } +} + abstract class StringIterator2 implements StringIterator {} void printItems3(StringIterator2 items) {