From 8cfde5e7984f705805c3bc364648cf27809cccb2 Mon Sep 17 00:00:00 2001 From: "Andrey.Cherkasov" Date: Thu, 18 Aug 2022 12:48:14 +0400 Subject: [PATCH] [java-intentions] ConvertSwitchToIf: don't add explicit null check if the selector expression is not null IDEA-300120 GitOrigin-RevId: d49fa3356b23ec87c8f493fc4cdacb0655e4b600 --- .../quickfix/ConvertSwitchToIfIntention.java | 6 +++++- .../quickFix/convertSwitchToIf/afterNotNull.java | 16 ++++++++++++++++ .../convertSwitchToIf/beforeNotNull.java | 14 ++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertSwitchToIf/afterNotNull.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertSwitchToIf/beforeNotNull.java diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ConvertSwitchToIfIntention.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ConvertSwitchToIfIntention.java index c5615695af4d..c89202f8f7f2 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ConvertSwitchToIfIntention.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ConvertSwitchToIfIntention.java @@ -2,9 +2,11 @@ package com.intellij.codeInsight.daemon.impl.quickfix; import com.intellij.codeInsight.BlockUtils; +import com.intellij.codeInsight.Nullability; import com.intellij.codeInsight.daemon.impl.actions.IntentionActionWithFixAllOption; import com.intellij.codeInsight.intention.FileModifier; import com.intellij.codeInspection.CommonQuickFixBundle; +import com.intellij.codeInspection.dataFlow.NullabilityUtil; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; import com.intellij.pom.java.LanguageLevel; @@ -116,7 +118,9 @@ public class ConvertSwitchToIfIntention implements IntentionActionWithFixAllOpti if (aClass != null) { String fqn = aClass.getQualifiedName(); useEquals = !aClass.isEnum() && (fqn == null || !TypeConversionUtil.isPrimitiveWrapper(fqn)); - needNullCheck = (fqn == null || !TypeConversionUtil.isPrimitiveWrapper(fqn)) && !hasNullCase(allBranches); + Nullability nullability = NullabilityUtil.getExpressionNullability(switchExpression, true); + needNullCheck = + nullability != Nullability.NOT_NULL && (fqn == null || !TypeConversionUtil.isPrimitiveWrapper(fqn)) && !hasNullCase(allBranches); } } diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertSwitchToIf/afterNotNull.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertSwitchToIf/afterNotNull.java new file mode 100644 index 000000000000..15237ca7fad6 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertSwitchToIf/afterNotNull.java @@ -0,0 +1,16 @@ +// "Replace 'switch' with 'if'" "true-preview" + +import org.jetbrains.annotations.NotNull; + +class Test { + + void foo(@NotNull Object o) { + if (o instanceof String) { + System.out.println("one"); + } else if (o instanceof Integer) { + System.out.println("two"); + } else { + System.out.println("default"); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertSwitchToIf/beforeNotNull.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertSwitchToIf/beforeNotNull.java new file mode 100644 index 000000000000..329b68bfa16a --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertSwitchToIf/beforeNotNull.java @@ -0,0 +1,14 @@ +// "Replace 'switch' with 'if'" "true-preview" + +import org.jetbrains.annotations.NotNull; + +class Test { + + void foo(@NotNull Object o) { + switch (o) { + case String s -> System.out.println("one"); + case Integer i -> System.out.println("two"); + case default -> System.out.println("default"); + } + } +} \ No newline at end of file