DFA - excluded boolean fix suggestion in conditional expressions which contain null expression types to prevent compilation error

GitOrigin-RevId: b0b956ad677ce1ecdc80070e25877aef8dc65100
This commit is contained in:
Ilyas Selimov
2021-05-14 17:22:38 +00:00
committed by intellij-monorepo-bot
parent 15070abb9b
commit d82ff9650f
8 changed files with 89 additions and 9 deletions
@@ -9,6 +9,7 @@ import com.intellij.codeInsight.intention.impl.BaseIntentionAction;
import com.intellij.codeInsight.intention.impl.SplitConditionUtil;
import com.intellij.codeInspection.CommonQuickFixBundle;
import com.intellij.codeInspection.LocalQuickFixOnPsiElement;
import com.intellij.codeInspection.dataFlow.NullabilityProblemKind;
import com.intellij.codeInspection.util.IntentionFamilyName;
import com.intellij.codeInspection.util.IntentionName;
import com.intellij.java.JavaBundle;
@@ -23,10 +24,7 @@ import com.intellij.psi.controlFlow.ControlFlowUtil;
import com.intellij.psi.scope.PatternResolveState;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.TokenSet;
import com.intellij.psi.util.JavaPsiPatternUtil;
import com.intellij.psi.util.PsiExpressionTrimRenderer;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.*;
import com.intellij.refactoring.util.RefactoringUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.IncorrectOperationException;
@@ -37,10 +35,7 @@ import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.*;
public class SimplifyBooleanExpressionFix extends LocalQuickFixOnPsiElement {
private static final Logger LOG = Logger.getInstance(SimplifyBooleanExpressionFix.class);
@@ -128,7 +123,14 @@ public class SimplifyBooleanExpressionFix extends LocalQuickFixOnPsiElement {
if (parent instanceof PsiDoWhileStatement && containsBreakOrContinue((PsiDoWhileStatement)parent)) {
return false;
}
PsiConditionalExpression parentConditionalExpr = ObjectUtils.tryCast(parent, PsiConditionalExpression.class);
if (parentConditionalExpr != null && NullabilityProblemKind.fromContext(parentConditionalExpr, Collections.emptyMap()) != null) {
PsiExpression exprToCheck = mySubExpressionValue ? parentConditionalExpr.getThenExpression()
: parentConditionalExpr.getElseExpression();
if (exprToCheck != null && TypeConversionUtil.isNullType(exprToCheck.getType())) {
return false;
}
}
return true;
}
@@ -0,0 +1,7 @@
// "Simplify 'arg.isEmpty()' to false" "true"
class Test {
void test() {
String arg = "12";
Integer result = 2 * 1;
}
}
@@ -0,0 +1,7 @@
// "Simplify 'arg.isEmpty()' to false" "true"
class Test {
void test() {
String arg = "12";
String result = arg + null;
}
}
@@ -0,0 +1,7 @@
// "Simplify 'arg.isEmpty()' to false" "false"
class Test {
void test() {
String arg = "12";
Boolean result = arg != null && (<caret>arg.isEmpty() ? true : null);
}
}
@@ -0,0 +1,7 @@
// "Simplify 'arg.isEmpty()' to false" "false"
class Test {
void test() {
String arg = "12";
Integer result = 2 * (<caret>arg.isEmpty() ? 1 : null);
}
}
@@ -0,0 +1,7 @@
// "Simplify 'arg.isEmpty()' to false" "true"
class Test {
void test() {
String arg = "12";
Integer result = 2 * (<caret>arg.isEmpty() ? null : 1);
}
}
@@ -0,0 +1,7 @@
// "Simplify 'arg.isEmpty()' to false" "true"
class Test {
void test() {
String arg = "12";
String result = arg + (<caret>arg.isEmpty() ? 1 : null);
}
}
@@ -0,0 +1,36 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.java.codeInsight.daemon.quickFix;
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.dataFlow.DataFlowInspection;
import org.jetbrains.annotations.NotNull;
public class SimplifyBooleanExpressionFixTest extends LightQuickFixParameterizedTestCase {
@Override
protected LocalInspectionTool @NotNull [] configureLocalInspectionTools() {
DataFlowInspection inspection = new DataFlowInspection();
inspection.SUGGEST_NULLABLE_ANNOTATIONS = true;
return new LocalInspectionTool[]{inspection};
}
@Override
protected String getBasePath() {
return "/codeInsight/daemonCodeAnalyzer/quickFix/simplifyBooleanExpressionFix";
}
}