Java: Don't suggest to extract the trivial method that returns its parameter (IDEA-191899)

This commit is contained in:
Pavel Dolgov
2018-05-15 15:33:22 +03:00
parent c93271ef13
commit 6385a9311f
5 changed files with 49 additions and 5 deletions
@@ -730,6 +730,9 @@ public class DuplicatesFinder {
if (myMatchType == MatchType.EXACT || !(pattern instanceof PsiExpression) || !(candidate instanceof PsiExpression)) {
return false;
}
if (myPattern.length == 1 && isSameExpression(myPattern[0], (PsiExpression)pattern)) {
return false;
}
if (myPatternComplexityHolder == null) {
myPatternComplexityHolder = new ComplexityHolder(myPatternAsList);
@@ -774,6 +777,16 @@ public class DuplicatesFinder {
return match.putExtractedParameter(part1, part2);
}
private static boolean isSameExpression(@NotNull PsiElement context, @NotNull PsiExpression expression) {
if (context instanceof PsiExpression) {
return PsiUtil.skipParenthesizedExprDown((PsiExpression)context) == PsiUtil.skipParenthesizedExprDown(expression);
}
if (context instanceof PsiDeclarationStatement) {
PsiElement parent = PsiUtil.skipParenthesizedExprUp(expression.getParent());
return parent instanceof PsiLocalVariable && parent.getParent() == context;
}
return false;
}
private static boolean matchModifierList(PsiModifierList modifierList1, PsiModifierList modifierList2) {
if (!(modifierList1.getParent() instanceof PsiLocalVariable)) {
@@ -55,6 +55,7 @@ import com.intellij.refactoring.util.classMembers.ElementNeedsThis;
import com.intellij.refactoring.util.duplicates.*;
import com.intellij.util.ArrayUtil;
import com.intellij.util.IncorrectOperationException;
import com.intellij.util.ObjectUtils;
import com.intellij.util.VisibilityUtil;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.MultiMap;
@@ -1164,15 +1165,15 @@ public class ExtractMethodProcessor implements MatchProvider {
if (myOutputVariables.length == 1) {
final PsiExpression returnValue = insertedReturnStatement.getReturnValue();
if (returnValue instanceof PsiReferenceExpression) {
final PsiElement resolved = ((PsiReferenceExpression)returnValue).resolve();
if (resolved instanceof PsiLocalVariable && Comparing.strEqual(((PsiVariable)resolved).getName(), outVariableName)) {
final PsiVariable variable = ObjectUtils.tryCast(((PsiReferenceExpression)returnValue).resolve(), PsiVariable.class);
if (variable != null && Comparing.strEqual(variable.getName(), outVariableName)) {
final PsiStatement statement = PsiTreeUtil.getPrevSiblingOfType(insertedReturnStatement, PsiStatement.class);
if (statement instanceof PsiDeclarationStatement) {
final PsiElement[] declaredElements = ((PsiDeclarationStatement)statement).getDeclaredElements();
if (ArrayUtil.find(declaredElements, resolved) != -1) {
InlineUtil.inlineVariable((PsiVariable)resolved, ((PsiVariable)resolved).getInitializer(),
if (ArrayUtil.find(declaredElements, variable) != -1) {
InlineUtil.inlineVariable(variable, PsiUtil.skipParenthesizedExprDown(variable.getInitializer()),
(PsiReferenceExpression)returnValue);
resolved.delete();
variable.delete();
}
}
}
@@ -0,0 +1,11 @@
class Node {
String getType() { return ""; }
String foo(Node left, Node right) {
String leftType = left.getType();
<selection>String rightType = (right.getType());</selection>
String type = "A";
return leftType + rightType + type;
}
}
@@ -0,0 +1,15 @@
class Node {
String getType() { return ""; }
String foo(Node left, Node right) {
String leftType = newMethod(left);
String rightType = newMethod(right);
String type = "A";
return leftType + rightType + type;
}
private String newMethod(Node right) {
return right.getType();
}
}
@@ -808,6 +808,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
doDuplicatesTest();
}
public void testSuggestChangeSignatureTrivialMethod() throws Exception {
doDuplicatesTest();
}
public void testSuggestChangeSignaturePlusOneFolding() throws Exception {
doDuplicatesTest();
}