mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
suppress constant condition reporting for any expression involving getters (IDEA-93244)
This commit is contained in:
@@ -283,7 +283,7 @@ public class DataFlowInspection extends BaseLocalInspectionTool {
|
||||
createSimplifyToAssignmentFix()
|
||||
);
|
||||
}
|
||||
else if (shouldReportConditionAlwaysTrueOrFalse(psiAnchor, evaluatesToTrue) && !visitor.silenceConstantCondition(instruction)) {
|
||||
else if (shouldReportConditionAlwaysTrueOrFalse(psiAnchor, evaluatesToTrue) && !visitor.silenceConstantCondition(psiAnchor)) {
|
||||
final LocalQuickFix fix = createSimplifyBooleanExpressionFix(psiAnchor, evaluatesToTrue);
|
||||
String message = InspectionsBundle.message(underBinary ?
|
||||
"dataflow.message.constant.condition.when.reached" :
|
||||
|
||||
+11
-4
@@ -20,8 +20,10 @@ import com.intellij.codeInspection.dataFlow.instructions.*;
|
||||
import com.intellij.codeInspection.dataFlow.value.*;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.TypeConversionUtil;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.containers.FactoryMap;
|
||||
import gnu.trove.THashSet;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -36,7 +38,7 @@ import java.util.Set;
|
||||
public class StandardInstructionVisitor extends InstructionVisitor {
|
||||
private final Set<BinopInstruction> myReachable = new THashSet<BinopInstruction>();
|
||||
private final Set<BinopInstruction> myCanBeNullInInstanceof = new THashSet<BinopInstruction>();
|
||||
private final Set<BinopInstruction> myNotToReportReachability = new THashSet<BinopInstruction>();
|
||||
private final Set<PsiElement> myNotToReportReachability = new THashSet<PsiElement>();
|
||||
private final Set<InstanceofInstruction> myUsefulInstanceofs = new THashSet<InstanceofInstruction>();
|
||||
private final FactoryMap<MethodCallInstruction, boolean[]> myParametersNotNull = new FactoryMap<MethodCallInstruction, boolean[]>() {
|
||||
@Override
|
||||
@@ -328,7 +330,7 @@ public class StandardInstructionVisitor extends InstructionVisitor {
|
||||
}
|
||||
|
||||
if (isViaMethods(dfaLeft) || isViaMethods(dfaRight)) {
|
||||
myNotToReportReachability.add(instruction);
|
||||
ContainerUtil.addIfNotNull(myNotToReportReachability, instruction.getPsiAnchor());
|
||||
}
|
||||
myCanBeNullInInstanceof.add(instruction);
|
||||
|
||||
@@ -435,7 +437,12 @@ public class StandardInstructionVisitor extends InstructionVisitor {
|
||||
return myCanBeNullInInstanceof.contains(instruction);
|
||||
}
|
||||
|
||||
public boolean silenceConstantCondition(BranchingInstruction instruction) {
|
||||
return instruction instanceof BinopInstruction && myNotToReportReachability.contains(instruction);
|
||||
public boolean silenceConstantCondition(@Nullable PsiElement element) {
|
||||
for (PsiElement skipped : myNotToReportReachability) {
|
||||
if (PsiTreeUtil.isAncestor(element, skipped, false)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
class Test {
|
||||
|
||||
private int count1;
|
||||
private int count2;
|
||||
|
||||
public void test() {
|
||||
int oldCount1 = getCount1();
|
||||
int oldCount2 = getCount2();
|
||||
count1++;
|
||||
if (oldCount1 != getCount1() || oldCount2 != getCount2()) {
|
||||
System.out.println("changed");
|
||||
}
|
||||
}
|
||||
|
||||
private int getCount1() {
|
||||
return count1;
|
||||
}
|
||||
|
||||
private int getCount2() {
|
||||
return count2;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -81,6 +81,7 @@ public class DataFlowInspectionFixtureTest extends JavaCodeInsightFixtureTestCas
|
||||
|
||||
public void testChainedFinalFieldsDfa() throws Throwable { doTest(); }
|
||||
public void testFinalFieldsDifferentInstances() throws Throwable { doTest(); }
|
||||
public void testThisFieldGetters() throws Throwable { doTest(); }
|
||||
public void testChainedFinalFieldAccessorsDfa() throws Throwable { doTest(); }
|
||||
|
||||
public void testAssigningUnknownToNullable() throws Throwable { doTest(); }
|
||||
|
||||
Reference in New Issue
Block a user