Bug 575571 - [17] Compiler wrongly considers switch to be enhanced
switch and reports error
Change-Id: I455fc3c39222d1e50539365b004ec704737c80e4
Signed-off-by: Jay Arthanareeswaran <jarthana@in.ibm.com>
Reviewed-on: https://git.eclipse.org/r/c/jdt/eclipse.jdt.core/+/184311
Tested-by: JDT Bot <jdt-bot@eclipse.org>
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchPatternTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchPatternTest.java
index 00c07b6..fd5ce24 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchPatternTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchPatternTest.java
@@ -3710,4 +3710,54 @@
"----------\n",
"");
}
+ public void testBug575571_1() {
+ Map<String, String> options = getCompilerOptions();
+ options.put(CompilerOptions.OPTION_ReportMissingDefaultCase, CompilerOptions.WARNING);
+ runWarningTest(
+ new String[] {
+ "X.java",
+ "public class X {\n" +
+ " public void foo(Color o) {\n" +
+ " switch (o) {\n" +
+ " case Blue:\n" +
+ " break;\n" +
+ " }\n" +
+ " }\n" +
+ " public static void main(String[] args) {}\n" +
+ "}\n" +
+ "enum Color { Blue; }\n",
+ },
+ "----------\n" +
+ "1. WARNING in X.java (at line 3)\n" +
+ " switch (o) {\n" +
+ " ^\n" +
+ "The switch over the enum type Color should have a default case\n" +
+ "----------\n",
+ options);
+ }
+ public void testBug575571_2() {
+ Map<String, String> options = getCompilerOptions();
+ options.put(CompilerOptions.OPTION_ReportMissingDefaultCase, CompilerOptions.WARNING);
+ runNegativeTest(
+ new String[] {
+ "X.java",
+ "public class X {\n" +
+ " public void foo(Color o) {\n" +
+ " switch (o) {\n" +
+ " case Blue:\n" +
+ " case Color c:\n" +
+ " break;\n" +
+ " }\n" +
+ " }\n" +
+ " public static void main(String[] args) {}\n" +
+ "}\n" +
+ "enum Color { Blue, Red; }\n",
+ },
+ "----------\n" +
+ "1. ERROR in X.java (at line 5)\n" +
+ " case Color c:\n" +
+ " ^^^^^^^^^^^^\n" +
+ "Illegal fall-through to a pattern\n" +
+ "----------\n");
+ }
}
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchStatement.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchStatement.java
index d424ec1..dd14a13 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchStatement.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/SwitchStatement.java
@@ -970,7 +970,7 @@
if (JavaFeature.PATTERN_MATCHING_IN_SWITCH.isSupported(upperScope.compilerOptions())
&& expressionType != null && !(this instanceof SwitchExpression )) {
- boolean acceptableType = true;
+ boolean acceptableType = !expressionType.isEnum();
switch (expressionType.id) {
case TypeIds.T_char:
case TypeIds.T_byte: