From 2aaba91ad5e5430d59327bebb463de4fb9c0d97a Mon Sep 17 00:00:00 2001 From: Greg Littlefield Date: Fri, 8 Sep 2023 14:39:36 -0700 Subject: [PATCH] Don't warn on keys based on enum values --- tools/analyzer_plugin/lib/src/diagnostic/bad_key.dart | 6 ++++-- .../test/integration/diagnostics/bad_key_test.dart | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/tools/analyzer_plugin/lib/src/diagnostic/bad_key.dart b/tools/analyzer_plugin/lib/src/diagnostic/bad_key.dart index 6ea263a76..f32329a78 100644 --- a/tools/analyzer_plugin/lib/src/diagnostic/bad_key.dart +++ b/tools/analyzer_plugin/lib/src/diagnostic/bad_key.dart @@ -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() ?.lookUpConcreteMethod('toString', element.library!) ?.thisOrAncestorOfType() ?.thisType .isDartCoreObject ?? - false; + false); } /// Recursively collects expressions that are used to effectively call `toString()`: diff --git a/tools/analyzer_plugin/test/integration/diagnostics/bad_key_test.dart b/tools/analyzer_plugin/test/integration/diagnostics/bad_key_test.dart index ea45a8001..d5c2974ea 100644 --- a/tools/analyzer_plugin/test/integration/diagnostics/bad_key_test.dart +++ b/tools/analyzer_plugin/test/integration/diagnostics/bad_key_test.dart @@ -47,6 +47,8 @@ class MyModelWithCustomToString { @override toString() => '$id'; } + +enum AnEnum { foo } '''; } @@ -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)(),