Merge pull request #918 from dart-lang/issue-32494
Handle undefined classes in rule (sdk issue 32494)
diff --git a/lib/src/rules/prefer_const_literals_to_create_immutables.dart b/lib/src/rules/prefer_const_literals_to_create_immutables.dart
index 3d880ad..cacafe1 100644
--- a/lib/src/rules/prefer_const_literals_to_create_immutables.dart
+++ b/lib/src/rules/prefer_const_literals_to_create_immutables.dart
@@ -83,8 +83,13 @@
}
}
- bool _hasImmutableAnnotation(ClassElement clazz) {
- final inheritedAndSelfTypes = _getSelfAndInheritedTypes(clazz.type);
+ bool _hasImmutableAnnotation(DartType type) {
+ if (type is! InterfaceType) {
+ // This happens when we find an instance creation expression for a class
+ // that cannot be resolved.
+ return false;
+ }
+ final inheritedAndSelfTypes = _getSelfAndInheritedTypes(type);
final inheritedAndSelfAnnotations = inheritedAndSelfTypes
.map((type) => type.element)
.expand((c) => c.metadata)
@@ -107,7 +112,7 @@
node = node.parent;
}
if (!(node is InstanceCreationExpression &&
- _hasImmutableAnnotation(node.bestType.element))) {
+ _hasImmutableAnnotation(node.bestType))) {
return;
}
diff --git a/test/rules/prefer_const_literals_to_create_immutables.dart b/test/rules/prefer_const_literals_to_create_immutables.dart
index 96e88f5..7bbac98 100644
--- a/test/rules/prefer_const_literals_to_create_immutables.dart
+++ b/test/rules/prefer_const_literals_to_create_immutables.dart
@@ -86,3 +86,6 @@
var m14 = new A({1: ''}); // LINT
var m15 = new A({1: null}); // LINT
+// ignore: undefined_class
+var e1 = new B([]); // OK
+