redundant cast: poly conditional expressions (IDEA-187764)

since 1.8 type of the (poly) conditional evaluates based on it's target type, so all checks on it's type are useless, instead check that operands expressions are compatible with the target type
This commit is contained in:
Anna.Kozlova
2018-03-14 17:39:04 +01:00
parent 6a9bc6782d
commit 62598b023f
2 changed files with 25 additions and 1 deletions
@@ -9,6 +9,7 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Comparing;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.psi.impl.source.resolve.graphInference.PsiPolyExpressionUtil;
import com.intellij.psi.infos.MethodCandidateInfo;
import com.intellij.psi.tree.IElementType;
import com.intellij.util.ArrayUtil;
@@ -406,7 +407,18 @@ public class RedundantCastUtil {
Comparing.equal(PsiUtil.recaptureWildcards(((PsiCallExpression)newCall).getType(), expression), ((PsiCallExpression)expression).getType())) &&
newResult.isValidResult() &&
!(newResult instanceof MethodCandidateInfo && ((MethodCandidateInfo)newResult).getInferenceErrorMessage() != null)) {
if (!(newArgs[i] instanceof PsiFunctionalExpression)) {
PsiExpression newArg = PsiUtil.deparenthesizeExpression(newArgs[i]);
if (newArg instanceof PsiConditionalExpression && PsiPolyExpressionUtil.isPolyExpression(newArgs[i])) {
PsiType targetType = newArg.getType();
LOG.assertTrue(targetType != null);
//target type is detected by method call
//check that both sides are fine with that
if (targetType.isAssignableFrom(((PsiConditionalExpression)newArg).getThenExpression().getType()) &&
targetType.isAssignableFrom(((PsiConditionalExpression)newArg).getElseExpression().getType())) {
addToResults(cast);
}
}
else if (!(newArg instanceof PsiFunctionalExpression)) {
addToResults(cast);
}
else {
@@ -3,3 +3,15 @@ class Test {
String kv = (String)(check ? "N" : objectArray[0]) ;
}
}
class PolyConditional {
public static void callee(String str) { }
public void usage(Object obj) {
callee((String) (obj != null ? obj : null));
}
public static void callee1(String str) { }
public void usage1(Object obj) {
callee1((String) (obj != null ? obj : null));
}
}