[java-highlighting] Remove unhelpful fixes when type inference fails

GitOrigin-RevId: a4da4b47eabbcf5438ff9d1de82240d4880b8c50
This commit is contained in:
Tagir Valeev
2023-01-13 15:14:43 +01:00
committed by intellij-monorepo-bot
parent b2dfb4c8a8
commit 35c501455e
4 changed files with 50 additions and 12 deletions

View File

@@ -216,15 +216,23 @@ final class AdaptExpressionTypeFixUtil {
@Nullable PsiType expectedType,
@Nullable PsiType actualType) {
if (actualType == null || expectedType == null) return;
boolean mentionsTypeArgument = mentionsTypeArgument(expression, actualType);
expectedType = GenericsUtil.getVariableTypeByExpressionType(expectedType);
TextRange range = expression.getTextRange();
String role = textRange.equals(range) ? null : getRole(expression);
IntentionAction action3 = new WrapWithAdapterMethodCallFix(expectedType, expression, role);
info.registerFix(action3, null, null, null, null);
IntentionAction action2 = QUICK_FIX_FACTORY.createWrapWithOptionalFix(expectedType, expression);
info.registerFix(action2, null, null, null, null);
IntentionAction action1 = new WrapExpressionFix(expectedType, expression, role);
info.registerFix(action1, null, null, null, null);
if (!mentionsTypeArgument) {
IntentionAction action3 = new WrapWithAdapterMethodCallFix(expectedType, expression, role);
info.registerFix(action3, null, null, null, null);
IntentionAction action2 = QUICK_FIX_FACTORY.createWrapWithOptionalFix(expectedType, expression);
info.registerFix(action2, null, null, null, null);
IntentionAction action1 = new WrapExpressionFix(expectedType, expression, role);
info.registerFix(action1, null, null, null, null);
PsiType castToType = suggestCastTo(expectedType, actualType);
if (castToType != null) {
IntentionAction action = new AddTypeCastFix(castToType, expression, role);
info.registerFix(action, null, null, null, null);
}
}
if (expectedType instanceof PsiArrayType) {
PsiType erasedValueType = TypeConversionUtil.erasure(actualType);
if (erasedValueType != null &&
@@ -234,11 +242,6 @@ final class AdaptExpressionTypeFixUtil {
}
}
HighlightFixUtil.registerCollectionToArrayFixAction(info, actualType, expectedType, expression);
PsiType castToType = suggestCastTo(expectedType, actualType);
if (castToType != null) {
IntentionAction action = new AddTypeCastFix(castToType, expression, role);
info.registerFix(action, null, null, null, null);
}
if (expression instanceof PsiMethodCallExpression call) {
PsiExpression qualifier = call.getMethodExpression().getQualifierExpression();
if (qualifier != null) {
@@ -254,6 +257,13 @@ final class AdaptExpressionTypeFixUtil {
}
}
private static boolean mentionsTypeArgument(PsiExpression expression, PsiType type) {
if (!(expression instanceof PsiMethodCallExpression call)) return false;
PsiMethod method = call.resolveMethod();
if (method == null) return false;
return PsiTypesUtil.mentionsTypeParameters(type, Set.of(method.getTypeParameters()));
}
@Nls
private static String getRole(@NotNull PsiExpression expression) {
PsiElement parent = PsiUtil.skipParenthesizedExprUp(expression.getParent());

View File

@@ -637,7 +637,9 @@ public final class HighlightMethodUtil {
}
if (methodCall instanceof PsiMethodCallExpression) {
registerMethodCallIntentions(builder, (PsiMethodCallExpression)methodCall, ((PsiMethodCallExpression)methodCall).getArgumentList(), resolveHelper);
registerMethodReturnFixAction(builder, resolveResult, methodCall);
if (!PsiTypesUtil.mentionsTypeParameters(actualType, Set.of(method.getTypeParameters()))) {
registerMethodReturnFixAction(builder, resolveResult, methodCall);
}
registerTargetTypeFixesBasedOnApplicabilityInference((PsiMethodCallExpression)methodCall, resolveResult, method, builder);
}
return builder;