[java-inspections] IDEA-356945 'insert default branch' suggests uncompilable code

GitOrigin-RevId: 44f277da31601775e3ce75021155bf7c37a6311c
This commit is contained in:
Mikhail Pyltsin
2024-08-01 12:29:33 +00:00
committed by intellij-monorepo-bot
parent 8e4ac5ef4b
commit 4a9f1b4fee
3 changed files with 19 additions and 2 deletions
@@ -951,6 +951,7 @@ public class PatternsInSwitchBlockHighlightingModel extends SwitchBlockHighlight
AtomicBoolean reported = new AtomicBoolean();
if (switchModel instanceof PatternsInSwitchBlockHighlightingModel patternsInSwitchModel) {
if (findUnconditionalPatternForType(labelElements, switchModel.mySelectorType) != null) return COMPLETE_WITH_UNCONDITIONAL;
if (switchModel.getSwitchSelectorKind() == SelectorKind.BOOLEAN && hasTrueAndFalse(labelElements)) return COMPLETE_WITH_UNCONDITIONAL;
if (!needToCheckCompleteness && !isEnumSelector) return INCOMPLETE;
//it is necessary,
// because deconstruction patterns don't cover cases when some of their components are null and deconstructionPattern too
@@ -0,0 +1,10 @@
package com.siyeh.igtest.controlflow.switch_statements_without_default;
class TrueFalsePatterns {
static void foo(Boolean b) {
switch (b) {
case true -> System.out.println("It's true");
case false -> System.out.println("It's false");
}
}
}
@@ -24,7 +24,13 @@ public class SwitchStatementsWithPatternsInspectionTest extends LightJavaInspect
myInspection.m_ignoreFullyCoveredEnums = false;
doTest();
}
public void testTrueFalsePatterns() {
String name = getTestName(false);
myFixture.configureByFile(name + ".java");
myFixture.testHighlighting(true, true, true);
}
@Nullable
@Override
protected InspectionProfileEntry getInspection() {
@@ -33,6 +39,6 @@ public class SwitchStatementsWithPatternsInspectionTest extends LightJavaInspect
@Override
protected @NotNull LightProjectDescriptor getProjectDescriptor() {
return JAVA_21;
return JAVA_23;
}
}