mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
disable cast suggestion for incompatible return type (IDEA-186479)
This commit is contained in:
+8
-1
@@ -23,6 +23,7 @@ import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.infos.CandidateInfo;
|
||||
import com.intellij.psi.util.PsiTypesUtil;
|
||||
import com.intellij.psi.util.TypeConversionUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import gnu.trove.THashSet;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -78,6 +79,7 @@ public abstract class ArgumentFixerActionFactory {
|
||||
if (methodCandidates.isEmpty()) return;
|
||||
|
||||
try {
|
||||
PsiType expectedTypeByParent = PsiTypesUtil.getExpectedTypeByParent(call);
|
||||
for (int i = 0; i < expressions.length; i++) {
|
||||
PsiExpression expression = expressions[i];
|
||||
PsiType exprType = expression.getType();
|
||||
@@ -99,7 +101,8 @@ public abstract class ArgumentFixerActionFactory {
|
||||
}
|
||||
// strict compare since even widening cast may help
|
||||
if (Comparing.equal(exprType, parameterType)) continue;
|
||||
PsiCall newCall = (PsiCall) call.copy();
|
||||
PsiCall newCall = LambdaUtil.copyTopLevelCall(call); //copy with expected type
|
||||
if (newCall == null) continue;
|
||||
PsiExpression modifiedExpression = getModifiedArgument(expression, parameterType);
|
||||
if (modifiedExpression == null) continue;
|
||||
PsiExpressionList argumentList = newCall.getArgumentList();
|
||||
@@ -107,6 +110,10 @@ public abstract class ArgumentFixerActionFactory {
|
||||
argumentList.getExpressions()[i].replace(modifiedExpression);
|
||||
JavaResolveResult resolveResult = newCall.resolveMethodGenerics();
|
||||
if (resolveResult.getElement() != null && resolveResult.isValidResult()) {
|
||||
if (expectedTypeByParent != null && newCall instanceof PsiCallExpression) {
|
||||
PsiType type = ((PsiCallExpression)newCall).getType();
|
||||
if (type != null && !TypeConversionUtil.isAssignable(expectedTypeByParent, type)) continue;
|
||||
}
|
||||
suggestedCasts.add(parameterType.getCanonicalText());
|
||||
QuickFixAction.registerQuickFixAction(highlightInfo, fixRange, createFix(list, i, parameterType));
|
||||
}
|
||||
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// "Cast parameter to 'java.util.List<A>'" "false"
|
||||
|
||||
import java.util.*;
|
||||
|
||||
class A { }
|
||||
|
||||
class B extends A { }
|
||||
|
||||
class C {
|
||||
public B getStrings(ArrayList<A> l) {
|
||||
return getFi<caret>rstItem(l);
|
||||
}
|
||||
|
||||
private static <T> T getFirstItem(List<T> items) { return null; }
|
||||
private static <T> T getFirstItem(Collection<T> items) { return null; }
|
||||
}
|
||||
Reference in New Issue
Block a user