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

Commit 84046a1

Browse files
authored
Merge pull request #918 from dart-lang/issue-32494
Handle undefined classes in rule (sdk issue 32494)
2 parents 0a08840 + 5a21462 commit 84046a1

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

lib/src/rules/prefer_const_literals_to_create_immutables.dart

+8-3
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,13 @@ class Visitor extends SimpleAstVisitor {
8383
}
8484
}
8585

86-
bool _hasImmutableAnnotation(ClassElement clazz) {
87-
final inheritedAndSelfTypes = _getSelfAndInheritedTypes(clazz.type);
86+
bool _hasImmutableAnnotation(DartType type) {
87+
if (type is! InterfaceType) {
88+
// This happens when we find an instance creation expression for a class
89+
// that cannot be resolved.
90+
return false;
91+
}
92+
final inheritedAndSelfTypes = _getSelfAndInheritedTypes(type);
8893
final inheritedAndSelfAnnotations = inheritedAndSelfTypes
8994
.map((type) => type.element)
9095
.expand((c) => c.metadata)
@@ -107,7 +112,7 @@ class Visitor extends SimpleAstVisitor {
107112
node = node.parent;
108113
}
109114
if (!(node is InstanceCreationExpression &&
110-
_hasImmutableAnnotation(node.bestType.element))) {
115+
_hasImmutableAnnotation(node.bestType))) {
111116
return;
112117
}
113118

test/rules/prefer_const_literals_to_create_immutables.dart

+3
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,6 @@ var m13 = new A({1: 1.0}); // LINT
8686
var m14 = new A({1: ''}); // LINT
8787
var m15 = new A({1: null}); // LINT
8888

89+
// ignore: undefined_class
90+
var e1 = new B([]); // OK
91+

0 commit comments

Comments
 (0)