[java-dfa] IDEA-363270 Misleading warning for switch case when expression on Boolean values

GitOrigin-RevId: 449fdf6cb9137b7dcd038adca7b26ffc15f7d59a
This commit is contained in:
Tagir Valeev
2024-11-22 15:34:30 +00:00
committed by intellij-monorepo-bot
parent 6a6e2c2eca
commit 68e240546f
3 changed files with 16 additions and 0 deletions
@@ -1065,6 +1065,7 @@ public class ControlFlowAnalyzer extends JavaElementVisitor {
addInstruction(new GotoInstruction(endGuardOffset));
((DeferredOffset)targetOffset).setOffset(getInstructionCount());
guard.accept(this);
generateBoxingUnboxingInstructionFor(guard, PsiTypes.booleanType());
addInstruction(new ResultOfInstruction(new JavaSwitchLabelTakenAnchor(guard)));
addInstruction(new ConditionalGotoInstruction(offset, DfTypes.TRUE));
endGuardOffset.setOffset(getInstructionCount());
@@ -0,0 +1,13 @@
import org.jetbrains.annotations.Nullable;
// IDEA-363270
public class SwitchBooleanWhen {
private String testBooleanValue(Object value) {
return switch (value) {
case Boolean booleanValue when booleanValue -> "true";
case Boolean booleanValue when <warning descr="Condition '!booleanValue' is always 'true'">!booleanValue</warning> -> "false";
case null -> "null";
default -> "unknown";
};
}
}
@@ -135,6 +135,8 @@ public class DataFlowInspection21Test extends DataFlowInspectionTestCase {
public void testInstanceOfPatternAffectNullity() { doTest(); }
public void testNullabilityInEnumSwitch() { doTest(); }
public void testSwitchBooleanWhen() { doTest(); }
public void testJetBrainsNotNullByDefault() {
addJetBrainsNotNullByDefault(myFixture);