Skip to content

Commit

Permalink
Don't warn on keys based on enum values
Browse files Browse the repository at this point in the history
  • Loading branch information
greglittlefield-wf committed Sep 8, 2023
1 parent 9bcf9b0 commit 2aaba91
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
6 changes: 4 additions & 2 deletions tools/analyzer_plugin/lib/src/diagnostic/bad_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,15 @@ class BadKeyDiagnostic extends ComponentUsageDiagnosticContributor {
}

static bool inheritsToStringImplFromObject(Element element) =>
element
// Enums have toStrings that include their name.
element is! EnumElement &&
(element
.tryCast<InterfaceElement>()
?.lookUpConcreteMethod('toString', element.library!)
?.thisOrAncestorOfType<InterfaceElement>()
?.thisType
.isDartCoreObject ??
false;
false);
}

/// Recursively collects expressions that are used to effectively call `toString()`:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class MyModelWithCustomToString {
@override
toString() => '$id';
}
enum AnEnum { foo }
''';
}

Expand All @@ -59,6 +61,7 @@ class BadKeyDiagnosticTest_NoErrors extends BadKeyDiagnosticTest {
final source = newSourceWithPrefix(/*language=dart*/ r'''
test() => [
(Dom.div()..key = 'a string')(),
(Dom.div()..key = AnEnum.foo)(),
(Dom.div()..key = 122)(),
(Dom.div()..key = modelVar.id)(),
(Dom.div()..key = modelVarWithCustomToString)(),
Expand Down

0 comments on commit 2aaba91

Please sign in to comment.