mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-05-06 13:20:53 +07:00
DataFlowInspection: npe fixes fixed
Fixes duplicating NPE expression are disabled when expression has side-effect (not when it's a method call) Parentheses added to generated code when necessary
This commit is contained in:
@@ -22,6 +22,7 @@ import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.CodeStyleManager;
|
||||
import com.intellij.psi.util.PsiTypesUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.siyeh.ig.psiutils.ParenthesesUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
@@ -37,7 +38,7 @@ public class ReplaceWithTernaryOperatorFix implements LocalQuickFix {
|
||||
}
|
||||
|
||||
public ReplaceWithTernaryOperatorFix(@NotNull PsiExpression expressionToAssert) {
|
||||
myText = expressionToAssert.getText();
|
||||
myText = ParenthesesUtils.getText(expressionToAssert, ParenthesesUtils.BINARY_AND_PRECEDENCE);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -48,6 +48,7 @@ import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.containers.MultiMap;
|
||||
import com.siyeh.ig.psiutils.ComparisonUtils;
|
||||
import com.siyeh.ig.psiutils.ExpressionUtils;
|
||||
import com.siyeh.ig.psiutils.SideEffectChecker;
|
||||
import com.siyeh.ig.psiutils.TypeUtils;
|
||||
import one.util.streamex.StreamEx;
|
||||
import org.jdom.Element;
|
||||
@@ -267,7 +268,7 @@ public class DataFlowInspectionBase extends BaseJavaBatchLocalInspectionTool {
|
||||
if (isVolatileFieldReference(qualifier)) {
|
||||
ContainerUtil.addIfNotNull(fixes, createIntroduceVariableFix(qualifier));
|
||||
}
|
||||
else if (!isNullLiteral(qualifier) && !(qualifier instanceof PsiMethodCallExpression)) {
|
||||
else if (!isNullLiteral(qualifier) && !SideEffectChecker.mayHaveSideEffects(qualifier)) {
|
||||
if (PsiUtil.getLanguageLevel(qualifier).isAtLeast(LanguageLevel.JDK_1_4)) {
|
||||
final Project project = qualifier.getProject();
|
||||
final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory();
|
||||
|
||||
@@ -27,6 +27,7 @@ import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtilBase;
|
||||
import com.intellij.refactoring.util.RefactoringUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.siyeh.ig.psiutils.ParenthesesUtils;
|
||||
import com.siyeh.ipp.trivialif.MergeIfAndIntention;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -45,7 +46,7 @@ public class SurroundWithIfFix implements LocalQuickFix {
|
||||
}
|
||||
|
||||
public SurroundWithIfFix(@NotNull PsiExpression expressionToAssert) {
|
||||
myText = expressionToAssert.getText();
|
||||
myText = ParenthesesUtils.getText(expressionToAssert, ParenthesesUtils.BINARY_AND_PRECEDENCE);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
// "Replace with '(b ? null : "foo") != null ?:'" "true"
|
||||
class A {
|
||||
void bar(String s) {}
|
||||
|
||||
void foo(boolean b){
|
||||
bar((b ? null : "foo") != null ? b ? null : "foo" : null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// "Replace with '(b ? null : "foo") != null ?:'" "true"
|
||||
class A {
|
||||
void bar(String s) {}
|
||||
|
||||
void foo(boolean b){
|
||||
bar(b ? null<caret> : "foo");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// "Surround with 'if ((b ? null : "foo") != null)'" "true"
|
||||
class A {
|
||||
void bar(String s) {}
|
||||
|
||||
void foo(boolean b){
|
||||
if ((b ? null : "foo") != null) {
|
||||
bar(b ? null : "foo");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// "Surround with 'if ((Math.random() > 0.5 ? null : "foo") != null)'" "false"
|
||||
class A {
|
||||
void bar(String s) {}
|
||||
|
||||
void foo(boolean b){
|
||||
bar(Math.random() > 0.5 ? null<caret> : "foo");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// "Surround with 'if ((b ? null : "foo") != null)'" "true"
|
||||
class A {
|
||||
void bar(String s) {}
|
||||
|
||||
void foo(boolean b){
|
||||
bar(b ? null<caret> : "foo");
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,9 @@ public class ReplaceWithTernaryOperatorTest extends LightQuickFixParameterizedTe
|
||||
@NotNull
|
||||
@Override
|
||||
protected LocalInspectionTool[] configureLocalInspectionTools() {
|
||||
return new LocalInspectionTool[]{new DataFlowInspection(), new NullableStuffInspection()};
|
||||
DataFlowInspection dataFlowInspection = new DataFlowInspection();
|
||||
dataFlowInspection.SUGGEST_NULLABLE_ANNOTATIONS = true;
|
||||
return new LocalInspectionTool[]{dataFlowInspection, new NullableStuffInspection()};
|
||||
}
|
||||
|
||||
public void test() throws Exception {
|
||||
|
||||
@@ -25,7 +25,9 @@ public class SurroundWithIfFixTest extends LightQuickFixParameterizedTestCase {
|
||||
@NotNull
|
||||
@Override
|
||||
protected LocalInspectionTool[] configureLocalInspectionTools() {
|
||||
return new LocalInspectionTool[]{new DataFlowInspection()};
|
||||
DataFlowInspection inspection = new DataFlowInspection();
|
||||
inspection.SUGGEST_NULLABLE_ANNOTATIONS = true;
|
||||
return new LocalInspectionTool[]{inspection};
|
||||
}
|
||||
|
||||
public void test() throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user