From dadcc9addea157380aa324f2385ddac8a41480e7 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Fri, 5 Jun 2020 08:54:35 +0200 Subject: [PATCH] java: redundant type arguments: check that parent call substitutor remains the same (IDEA-241259) GitOrigin-RevId: 8e0e8d23e2868a47a857fb84bfb3a43fc34263f6 --- .../intellij/psi/impl/PsiDiamondTypeUtil.java | 15 ++++++++--- .../beforeQCallNotSameInference.java | 25 +++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantTypeArgs/beforeQCallNotSameInference.java diff --git a/java/java-psi-impl/src/com/intellij/psi/impl/PsiDiamondTypeUtil.java b/java/java-psi-impl/src/com/intellij/psi/impl/PsiDiamondTypeUtil.java index 5eaa5ef30ddf..dcd510084428 100644 --- a/java/java-psi-impl/src/com/intellij/psi/impl/PsiDiamondTypeUtil.java +++ b/java/java-psi-impl/src/com/intellij/psi/impl/PsiDiamondTypeUtil.java @@ -216,18 +216,18 @@ public class PsiDiamondTypeUtil { } } } + final PsiCallExpression exprCopy = PsiTreeUtil.getParentOfType(copy, PsiCallExpression.class, false); if (context instanceof PsiMethodReferenceExpression) { PsiMethodReferenceExpression methodRefCopy = PsiTreeUtil.getParentOfType(copy, PsiMethodReferenceExpression.class, false); if (methodRefCopy != null && !isInferenceEquivalent(typeArguments, typeParameters, method, methodRefCopy)) { return false; } - return true; } - final PsiCallExpression exprCopy = PsiTreeUtil.getParentOfType(copy, PsiCallExpression.class, false); - if (exprCopy != null) { + else if (exprCopy != null) { final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(exprCopy.getProject()); if (constructorRef) { - if (!(exprCopy instanceof PsiNewExpression) || !isInferenceEquivalent(typeArguments, elementFactory, (PsiNewExpression)exprCopy)) { + if (!(exprCopy instanceof PsiNewExpression) || + !isInferenceEquivalent(typeArguments, elementFactory, (PsiNewExpression)exprCopy)) { return false; } } @@ -238,6 +238,13 @@ public class PsiDiamondTypeUtil { } } } + + PsiCallExpression newParentCall = exprCopy != null ? PsiTreeUtil.getParentOfType(exprCopy, PsiCallExpression.class) : null; + PsiCallExpression oldParentCall = PsiTreeUtil.getParentOfType(context, PsiCallExpression.class); + if (newParentCall != null && oldParentCall != null && + !newParentCall.resolveMethodGenerics().equals(oldParentCall.resolveMethodGenerics())) { + return false; + } } catch (IncorrectOperationException e) { LOG.info(e); diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantTypeArgs/beforeQCallNotSameInference.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantTypeArgs/beforeQCallNotSameInference.java new file mode 100644 index 000000000000..0e4be1103403 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantTypeArgs/beforeQCallNotSameInference.java @@ -0,0 +1,25 @@ +// "Remove type arguments" "false" +import java.util.function.Function; + +class MyTest { + + public JBIterable getChildren(JBIterable children) { + return children + .map(this.ring>wrapper()) + .filter(); + } + + protected Function wrapper() { + return null; + } + + abstract class JBIterable{ + public final JBIterable map(Function function) { + return null; + } + + public final JBIterable filter() { + return null; + } + } +} \ No newline at end of file