suspicious method call: ignore poly conditional expressions in invocation context as their type would be always the same as target type (IDEA-155622)

This commit is contained in:
Anna.Kozlova
2016-05-09 19:42:35 +02:00
parent b0b98999b9
commit 48d81d53f0
5 changed files with 23 additions and 3 deletions
@@ -97,11 +97,11 @@ public class SuspiciousCollectionsMethodCallsInspection extends BaseJavaBatchLoc
PsiType argType = args[0].getType();
final String plainMessage = SuspiciousMethodCallUtil
.getSuspiciousMethodCallMessage(methodCall, argType, reportConvertibleMethodCalls, patternMethods, indices);
.getSuspiciousMethodCallMessage(methodCall, args[0], argType, reportConvertibleMethodCalls, patternMethods, indices);
if (plainMessage != null) {
final PsiType dfaType = GuessManager.getInstance(methodCall.getProject()).getControlFlowExpressionType(args[0]);
if (dfaType != null && SuspiciousMethodCallUtil
.getSuspiciousMethodCallMessage(methodCall, dfaType, reportConvertibleMethodCalls, patternMethods, indices) == null) {
.getSuspiciousMethodCallMessage(methodCall, args[0], dfaType, reportConvertibleMethodCalls, patternMethods, indices) == null) {
return null;
}
}
@@ -18,6 +18,7 @@ package com.intellij.codeInspection.miscGenerics;
import com.intellij.codeInsight.daemon.impl.analysis.JavaGenericsUtil;
import com.intellij.codeInspection.InspectionsBundle;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.resolve.graphInference.PsiPolyExpressionUtil;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.util.*;
import com.intellij.util.containers.IntArrayList;
@@ -104,6 +105,7 @@ public class SuspiciousMethodCallUtil {
@Nullable
public static String getSuspiciousMethodCallMessage(@NotNull PsiMethodCallExpression methodCall,
PsiExpression arg,
PsiType argType,
boolean reportConvertibleMethodCalls,
@NotNull List<PsiMethod> patternMethods,
@@ -117,6 +119,12 @@ public class SuspiciousMethodCallUtil {
if (argType == null) return null;
if (arg instanceof PsiConditionalExpression &&
PsiPolyExpressionUtil.isPolyExpression(arg) &&
argType.equalsToText(CommonClassNames.JAVA_LANG_OBJECT)) {
return null;
}
final JavaResolveResult resolveResult = methodExpression.advancedResolve(false);
PsiMethod calleeMethod = (PsiMethod)resolveResult.getElement();
if (calleeMethod == null) return null;
@@ -116,7 +116,7 @@ public class RedundantCastInspection extends GenericsInspectionToolBase {
final PsiElement gParent = parent.getParent();
if (gParent instanceof PsiMethodCallExpression && IGNORE_SUSPICIOUS_METHOD_CALLS) {
final String message = SuspiciousMethodCallUtil
.getSuspiciousMethodCallMessage((PsiMethodCallExpression)gParent, operand.getType(), true, new ArrayList<PsiMethod>(),
.getSuspiciousMethodCallMessage((PsiMethodCallExpression)gParent, operand, operand.getType(), true, new ArrayList<PsiMethod>(),
new IntArrayList());
if (message != null) {
return null;
@@ -0,0 +1,11 @@
import java.util.List;
import java.util.Map;
class Test {
void m(Map<Class, String> map) {
map.get(map.size() > 10 ? Map.class : List.class);
}
}
@@ -24,6 +24,7 @@ public class SuspiciousCollectionMethodCallsTest extends LightCodeInsightFixture
public void testSetList() throws Exception { doTest(); }
public void testUseDfa() throws Exception { doTest(); }
public void testWildcard() throws Exception { doTest(); }
public void testPolyConditionalExpressionPassedToMapGetCall() throws Exception { doTest(); }
public void testIgnoreConvertible() throws Exception {
myTool.REPORT_CONVERTIBLE_METHOD_CALLS = false;
doTest();