Skip to content

Commit 1863250

Browse files
authored
Add new problem for enum switch expression incomplete despite default (#4373)
- add a new SwitchExpressionMissingEnumConstantCaseDespiteDefault problem id to IProblem - add a new corresponding message to messages.properties - add new problem to ProblemReporter.getIrritant() - add new test to ProgrammingProblemsTest - modify CompilerInvocationTests - fixes #4328
1 parent ec96ae6 commit 1863250

File tree

9 files changed

+47
-5
lines changed

9 files changed

+47
-5
lines changed

org.eclipse.jdt.core.compiler.batch/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Main-Class: org.eclipse.jdt.internal.compiler.batch.Main
33
Bundle-ManifestVersion: 2
44
Bundle-Name: Eclipse Compiler for Java(TM)
55
Bundle-SymbolicName: org.eclipse.jdt.core.compiler.batch
6-
Bundle-Version: 3.43.100.qualifier
6+
Bundle-Version: 3.44.0.qualifier
77
Bundle-ClassPath: .
88
Bundle-Vendor: Eclipse.org
99
Automatic-Module-Name: org.eclipse.jdt.core.compiler.batch

org.eclipse.jdt.core.compiler.batch/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<version>4.38.0-SNAPSHOT</version>
1818
</parent>
1919
<artifactId>org.eclipse.jdt.core.compiler.batch</artifactId>
20-
<version>3.43.100-SNAPSHOT</version>
20+
<version>3.44.0-SNAPSHOT</version>
2121
<packaging>eclipse-plugin</packaging>
2222

2323
<properties>

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/core/compiler/IProblem.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2495,6 +2495,8 @@ public interface IProblem {
24952495
int SwitchExpressionsContinueOutOfSwitchExpression = Syntax + 1723;
24962496
/** @since 3.22 */
24972497
int SwitchExpressionsReturnWithinSwitchExpression = Syntax + 1724;
2498+
/** @since 3.44 */
2499+
int SwitchExpressionMissingEnumConstantCaseDespiteDefault = FieldRelated + 1725;
24982500

24992501
/* records - begin */
25002502

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ public static int getIrritant(int problemID) {
443443
return CompilerOptions.AutoBoxing;
444444

445445
case IProblem.MissingEnumConstantCase :
446+
case IProblem.SwitchExpressionMissingEnumConstantCaseDespiteDefault :
446447
case IProblem.MissingEnumConstantCaseDespiteDefault : // this one is further protected by CompilerOptions.reportMissingEnumCaseDespiteDefault
447448
return CompilerOptions.MissingEnumConstantCase;
448449

@@ -6625,7 +6626,7 @@ public void missingEnumConstantCase(SwitchExpression switchExpression, FieldBind
66256626
}
66266627
private void missingSwitchExpressionEnumConstantCase(CaseStatement defaultCase, FieldBinding enumConstant, ASTNode expression) {
66276628
this.handle(
6628-
IProblem.SwitchExpressionsYieldMissingEnumConstantCase,
6629+
defaultCase == null ? IProblem.SwitchExpressionsYieldMissingEnumConstantCase : IProblem.SwitchExpressionMissingEnumConstantCaseDespiteDefault,
66296630
new String[] {new String(enumConstant.declaringClass.readableName()), new String(enumConstant.name) },
66306631
new String[] {new String(enumConstant.declaringClass.shortReadableName()), new String(enumConstant.name) },
66316632
expression.sourceStart,

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/problem/messages.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,8 @@
10631063
1722 = Breaking out of switch expressions not permitted
10641064
1723 = Continue out of switch expressions not permitted
10651065
1724 = Return within switch expressions not permitted
1066+
1725 = The enum constant {1} should have a corresponding case label in this enum switch on {0}. To suppress this problem, add a comment //$CASES-OMITTED$ on the line above the ''default:''
1067+
10661068

10671069

10681070
# Java 16 -- Records

org.eclipse.jdt.core.tests.compiler/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: %pluginName
44
Bundle-SymbolicName: org.eclipse.jdt.core.tests.compiler;singleton:=true
5-
Bundle-Version: 3.13.900.qualifier
5+
Bundle-Version: 3.13.1000.qualifier
66
Bundle-Vendor: %providerName
77
Bundle-Localization: plugin
88
Export-Package: org.eclipse.jdt.core.tests.compiler,

org.eclipse.jdt.core.tests.compiler/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<relativePath>../tests-pom/</relativePath>
2020
</parent>
2121
<artifactId>org.eclipse.jdt.core.tests.compiler</artifactId>
22-
<version>3.13.900-SNAPSHOT</version>
22+
<version>3.13.1000-SNAPSHOT</version>
2323
<packaging>eclipse-test-plugin</packaging>
2424

2525
<properties>

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,7 @@ class ProblemAttributes {
10711071
expectedProblemAttributes.put("SuperclassNotFound", DEPRECATED);
10721072
expectedProblemAttributes.put("SuperclassNotVisible", DEPRECATED);
10731073
expectedProblemAttributes.put("SuperfluousSemicolon", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
1074+
expectedProblemAttributes.put("SwitchExpressionMissingEnumConstantCaseDespiteDefault", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
10741075
expectedProblemAttributes.put("SwitchOnEnumNotBelow15", new ProblemAttributes(CategorizedProblem.CAT_TYPE));
10751076
expectedProblemAttributes.put("SwitchOnStringsNotBelow17", new ProblemAttributes(CategorizedProblem.CAT_TYPE));
10761077
expectedProblemAttributes.put("TargetTypeNotAFunctionalInterface", new ProblemAttributes(CategorizedProblem.CAT_TYPE));
@@ -2222,6 +2223,7 @@ class ProblemAttributes {
22222223
expectedProblemAttributes.put("SuperclassNotFound", SKIP);
22232224
expectedProblemAttributes.put("SuperclassNotVisible", new ProblemAttributes(JavaCore.COMPILER_PB_REDUNDANT_SUPERINTERFACE));
22242225
expectedProblemAttributes.put("SuperfluousSemicolon", new ProblemAttributes(JavaCore.COMPILER_PB_EMPTY_STATEMENT));
2226+
expectedProblemAttributes.put("SwitchExpressionMissingEnumConstantCaseDespiteDefault", new ProblemAttributes(JavaCore.COMPILER_PB_INCOMPLETE_ENUM_SWITCH));
22252227
expectedProblemAttributes.put("SwitchOnEnumNotBelow15", SKIP);
22262228
expectedProblemAttributes.put("SwitchOnStringsNotBelow17", SKIP);
22272229
expectedProblemAttributes.put("TargetTypeNotAFunctionalInterface", SKIP);

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ProgrammingProblemsTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4271,4 +4271,39 @@ record MyRecord(String value) {
42714271
true/*shouldFlushOutputDirectory*/,
42724272
customOptions);
42734273
}
4274+
//https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4328
4275+
public void testIssue4328() {
4276+
if (this.complianceLevel < ClassFileConstants.JDK14)
4277+
return;
4278+
Map customOptions = getCompilerOptions();
4279+
customOptions.put(CompilerOptions.OPTION_ReportIncompleteEnumSwitch, CompilerOptions.WARNING);
4280+
customOptions.put(CompilerOptions.OPTION_ReportMissingEnumCaseDespiteDefault, CompilerOptions.ENABLED);
4281+
this.runNegativeTest(
4282+
new String[] {
4283+
"IncompleteEnumDespiteDefaultWarning.java",
4284+
"""
4285+
public class IncompleteEnumDespiteDefaultWarning {
4286+
4287+
enum E { F, G, H }
4288+
static int testEnumExhaustive(E e) {
4289+
return switch(e) {
4290+
case F -> 0;
4291+
case G -> 1;
4292+
default -> 2;
4293+
};
4294+
}
4295+
}
4296+
4297+
"""
4298+
},
4299+
"----------\n" +
4300+
"1. WARNING in IncompleteEnumDespiteDefaultWarning.java (at line 5)\n" +
4301+
" return switch(e) {\n" +
4302+
" ^\n" +
4303+
"The enum constant H should have a corresponding case label in this enum switch on IncompleteEnumDespiteDefaultWarning.E. To suppress this problem, add a comment //$CASES-OMITTED$ on the line above the \'default:\'\n" +
4304+
"----------\n",
4305+
null/*classLibraries*/,
4306+
true/*shouldFlushOutputDirectory*/,
4307+
customOptions);
4308+
}
42744309
}

0 commit comments

Comments
 (0)