mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[java-dfa] IDEA-299239 Pattern nullity handling for Java 19
GitOrigin-RevId: ded6dcdf0f6b1cca8aec4a223290fc231fb67483
This commit is contained in:
committed by
intellij-monorepo-bot
parent
a9398fbb13
commit
7805e4dbb7
+6
-2
@@ -7,6 +7,7 @@ import com.intellij.codeInspection.dataFlow.java.ControlFlowAnalyzer;
|
||||
import com.intellij.codeInspection.dataFlow.jvm.problems.JvmDfaProblem;
|
||||
import com.intellij.codeInspection.util.InspectionMessage;
|
||||
import com.intellij.java.analysis.JavaAnalysisBundle;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.psi.util.JavaPsiPatternUtil;
|
||||
@@ -350,8 +351,11 @@ public final class NullabilityProblemKind<T extends PsiElement> {
|
||||
if (labelElementList == null) continue;
|
||||
for (PsiCaseLabelElement element : labelElementList.getElements()) {
|
||||
if (element instanceof PsiExpression && TypeConversionUtil.isNullType(((PsiExpression)element).getType())) return null;
|
||||
if (element instanceof PsiPattern && expressionType != null &&
|
||||
JavaPsiPatternUtil.isTotalForType(element, expressionType)) return null;
|
||||
if (PsiUtil.getLanguageLevel(element).isLessThan(LanguageLevel.JDK_19_PREVIEW) &&
|
||||
element instanceof PsiPattern && expressionType != null &&
|
||||
JavaPsiPatternUtil.isTotalForType(element, expressionType)) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-1
@@ -36,6 +36,7 @@ import com.intellij.codeInspection.dataFlow.types.DfTypes;
|
||||
import com.intellij.codeInspection.dataFlow.value.*;
|
||||
import com.intellij.codeInspection.dataFlow.value.DfaControlTransferValue.Trap;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
@@ -1115,7 +1116,8 @@ public class ControlFlowAnalyzer extends JavaElementVisitor {
|
||||
addInstruction(new JvmPushInstruction(expressionValue, null));
|
||||
|
||||
DeferredOffset condGotoOffset = null;
|
||||
if (!JavaPsiPatternUtil.isTotalForType(sourcePattern, checkType)) {
|
||||
if (!JavaPsiPatternUtil.isTotalForType(sourcePattern, checkType) ||
|
||||
!PsiUtil.getLanguageLevel(myCodeFragment).isLessThan(LanguageLevel.JDK_19_PREVIEW)) {
|
||||
addInstruction(new DupInstruction());
|
||||
addInstruction(new PushValueInstruction(DfTypes.typedObject(JavaPsiPatternUtil.getPatternType(innerPattern), Nullability.NOT_NULL)));
|
||||
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
class Test {
|
||||
void test(Object obj) {
|
||||
switch (obj) {
|
||||
case Object o -> {
|
||||
if (<warning descr="Condition 'o == null' is always 'false'">o == null</warning>) {}
|
||||
}
|
||||
}
|
||||
if (<warning descr="Condition 'obj == null' is always 'false'">obj == null</warning>) {}
|
||||
}
|
||||
|
||||
void test2(Object obj) {
|
||||
switch (obj) {
|
||||
case null -> {}
|
||||
case Object o -> {
|
||||
if (<warning descr="Condition 'o == null' is always 'false'">o == null</warning>) {}
|
||||
}
|
||||
}
|
||||
if (obj == null) {}
|
||||
}
|
||||
|
||||
void test3(Object obj) {
|
||||
switch (obj) {
|
||||
case Object o -> {
|
||||
if (<warning descr="Condition 'o == null' is always 'false'">o == null</warning>) {}
|
||||
}
|
||||
case null -> {}
|
||||
}
|
||||
if (obj == null) {}
|
||||
}
|
||||
}
|
||||
@@ -20,4 +20,7 @@ public class DataFlowInspection19Test extends DataFlowInspectionTestCase {
|
||||
public void testWhenPatterns() {
|
||||
doTest();
|
||||
}
|
||||
public void testSwitchNullability() {
|
||||
doTest();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user