diff --git a/pkg/linter/lib/src/rules/unnecessary_const_in_enum_constructor.dart b/pkg/linter/lib/src/rules/unnecessary_const_in_enum_constructor.dart index a1682b05d9e..2623c0fbe67 100644 --- a/pkg/linter/lib/src/rules/unnecessary_const_in_enum_constructor.dart +++ b/pkg/linter/lib/src/rules/unnecessary_const_in_enum_constructor.dart @@ -48,6 +48,7 @@ class _Visitor extends SimpleAstVisitor { @override void visitConstructorDeclaration(ConstructorDeclaration node) { + if (node.parent is! BlockEnumBody) return; var constKeyword = node.constKeyword; if (constKeyword != null) { rule.reportAtToken(constKeyword); @@ -56,6 +57,7 @@ class _Visitor extends SimpleAstVisitor { @override void visitPrimaryConstructorDeclaration(PrimaryConstructorDeclaration node) { + if (node.parent is! EnumDeclaration) return; var constKeyword = node.constKeyword; if (constKeyword != null) { rule.reportAtToken(constKeyword); diff --git a/pkg/linter/test/rules/unnecessary_const_in_enum_constructor_test.dart b/pkg/linter/test/rules/unnecessary_const_in_enum_constructor_test.dart index dc007d7ebfc..4387eb847d8 100644 --- a/pkg/linter/test/rules/unnecessary_const_in_enum_constructor_test.dart +++ b/pkg/linter/test/rules/unnecessary_const_in_enum_constructor_test.dart @@ -17,7 +17,13 @@ class UnnecessaryConstInEnumConstructorTest extends LintRuleTest { @override String get lintRule => LintNames.unnecessary_const_in_enum_constructor; - test_primary() async { + test_primary_inClass() async { + await assertNoDiagnostics(r''' +class const C(); +'''); + } + + test_primary_inEnum() async { await assertDiagnosticsFromMarkdown(r''' enum [!const!] E(final int i) { a(1), b(2); @@ -25,7 +31,15 @@ enum [!const!] E(final int i) { '''); } - test_secondary() async { + test_secondary_inClass() async { + await assertNoDiagnostics(r''' +class C { + const C(); +} +'''); + } + + test_secondary_inEnum() async { await assertDiagnosticsFromMarkdown(r''' enum E { a(1), b(2);