diff --git a/pkg/analyzer/lib/src/generated/error_verifier.dart b/pkg/analyzer/lib/src/generated/error_verifier.dart index b93727e2c3d4..bd4b3308f70e 100644 --- a/pkg/analyzer/lib/src/generated/error_verifier.dart +++ b/pkg/analyzer/lib/src/generated/error_verifier.dart @@ -3273,7 +3273,7 @@ class ErrorVerifier extends RecursiveAstVisitor var enumElement = expressionType.element; if (enumElement.isEnum) { var constantNames = enumElement.fields - .where((field) => field.isStatic && !field.isSynthetic) + .where((field) => field.isEnumConstant) .map((field) => field.name) .toSet(); diff --git a/pkg/analyzer/lib/src/generated/resolver.dart b/pkg/analyzer/lib/src/generated/resolver.dart index 78180a7adab4..a0e5fa347d18 100644 --- a/pkg/analyzer/lib/src/generated/resolver.dart +++ b/pkg/analyzer/lib/src/generated/resolver.dart @@ -3824,6 +3824,8 @@ class _SwitchExhaustiveness { return expression.staticElement; } else if (expression is PropertyAccess) { return expression.propertyName.staticElement; + } else if (expression is SimpleIdentifier) { + return expression.staticElement; } return null; } diff --git a/pkg/analyzer/test/src/diagnostics/body_might_complete_normally_test.dart b/pkg/analyzer/test/src/diagnostics/body_might_complete_normally_test.dart index 973910e51beb..649225f94cd4 100644 --- a/pkg/analyzer/test/src/diagnostics/body_might_complete_normally_test.dart +++ b/pkg/analyzer/test/src/diagnostics/body_might_complete_normally_test.dart @@ -15,6 +15,42 @@ main() { @reflectiveTest class BodyMayCompleteNormallyTest extends PubPackageResolutionTest { + test_enum_method_nonNullable_blockBody_switchStatement_notNullable_exhaustive() async { + await assertNoErrorsInCode(r''' +enum E { + a; + + static const b = 0; + static final c = 0; + + int get value { + switch (this) { + case a: + return 0; + } + } +} +'''); + } + + test_enum_method_nonNullable_blockBody_switchStatement_notNullable_notExhaustive() async { + await assertErrorsInCode(r''' +enum E { + a, b; + + int get value { + switch (this) { + case a: + return 0; + } + } +} +''', [ + error(CompileTimeErrorCode.BODY_MIGHT_COMPLETE_NORMALLY, 28, 5), + error(StaticWarningCode.MISSING_ENUM_CONSTANT_IN_SWITCH, 40, 13), + ]); + } + test_factoryConstructor_named_blockBody() async { await assertErrorsInCode(r''' class A { @@ -92,6 +128,24 @@ int f(Foo foo) { '''); } + test_function_nonNullable_blockBody_switchStatement_notNullable_exhaustive_enhanced() async { + await assertNoErrorsInCode(r''' +enum E { + a; + + static const b = 0; + static final c = 0; +} + +int f(E e) { + switch (e) { + case E.a: + return 0; + } +} +'''); + } + test_function_nonNullable_blockBody_switchStatement_notNullable_notExhaustive() async { await assertErrorsInCode(r''' enum Foo { a, b } @@ -108,6 +162,26 @@ int f(Foo foo) { ]); } + test_function_nonNullable_blockBody_switchStatement_notNullable_notExhaustive_enhanced() async { + await assertErrorsInCode(r''' +enum E { + a, b; + + static const c = 0; +} + +int f(E e) { + switch (e) { + case E.a: + return 0; + } +} +''', [ + error(CompileTimeErrorCode.BODY_MIGHT_COMPLETE_NORMALLY, 47, 1), + error(StaticWarningCode.MISSING_ENUM_CONSTANT_IN_SWITCH, 58, 10), + ]); + } + test_function_nonNullable_blockBody_switchStatement_nullable_exhaustive_default() async { await assertNoErrorsInCode(r''' enum Foo { a, b } diff --git a/pkg/analyzer/test/src/diagnostics/missing_enum_constant_in_switch_test.dart b/pkg/analyzer/test/src/diagnostics/missing_enum_constant_in_switch_test.dart index 7ea897235640..dd74912de7d0 100644 --- a/pkg/analyzer/test/src/diagnostics/missing_enum_constant_in_switch_test.dart +++ b/pkg/analyzer/test/src/diagnostics/missing_enum_constant_in_switch_test.dart @@ -17,6 +17,25 @@ main() { @reflectiveTest class MissingEnumConstantInSwitchTest extends PubPackageResolutionTest with MissingEnumConstantInSwitchTestCases { + test_all_enhanced() async { + await assertNoErrorsInCode(''' +enum E { + one, two; + + static const x = 0; +} + +void f(E e) { + switch (e) { + case E.one: + break; + case E.two: + break; + } +} +'''); + } + test_nullable() async { await assertErrorsInCode(''' enum E { one, two }