[java-dfa] Test for IDEA-357456

GitOrigin-RevId: b7b97300c5a4512009b006cdfafeba56bcfe4c69
This commit is contained in:
Tagir Valeev
2024-09-02 16:26:17 +02:00
committed by intellij-monorepo-bot
parent 49f0880f57
commit 84ce93ddc5
2 changed files with 31 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
import org.jetbrains.annotations.Nullable;
// IDEA-357456
public class NullabilityInEnumSwitch {
enum Option {
A(false),
B(true),
C(true);
private final boolean needsValue;
Option(boolean needsValue) {
this.needsValue = needsValue;
}
}
static boolean test(@Nullable String value, Option option) {
if (option.needsValue && value == null) throw new IllegalArgumentException();
return switch (option) {
case A -> true;
// Too hard to analyze: we should go back into needsValue initializer
case B -> value.<warning descr="Method invocation 'isEmpty' may produce 'NullPointerException'">isEmpty</warning>();
case C -> value.<warning descr="Method invocation 'startsWith' may produce 'NullPointerException'">startsWith</warning>("a");
};
}
}

View File

@@ -133,4 +133,6 @@ public class DataFlowInspection21Test extends DataFlowInspectionTestCase {
public void testArrayAddedIntoCollection() { doTest(); }
public void testInstanceOfPatternAffectNullity() { doTest(); }
public void testNullabilityInEnumSwitch() { doTest(); }
}