ChainCallJoinLinesHandler: disable when builder variable is self-referenced

This commit is contained in:
Tagir Valeev
2018-04-19 13:40:39 +07:00
parent deeec13f53
commit cee5209a63
4 changed files with 19 additions and 0 deletions
@@ -4,6 +4,8 @@ package com.intellij.codeInsight.editorActions;
import com.intellij.codeInsight.PsiEquivalenceUtil;
import com.intellij.openapi.editor.Document;
import com.intellij.psi.*;
import com.intellij.psi.search.LocalSearchScope;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.util.PsiTreeUtil;
import com.siyeh.ig.psiutils.ExpressionUtils;
import org.jetbrains.annotations.NotNull;
@@ -53,6 +55,7 @@ public class ChainCallJoinLinesHandler implements JoinLinesHandlerDelegate {
if (!ExpressionUtils.isReferenceTo(qualifier, var)) return false;
PsiType type = nextCall.getType();
if (type == null || !type.equals(initializer.getType())) return false;
if (ReferencesSearch.search(var, new LocalSearchScope(nextCall)).findAll().size() > 1) return false;
qualifier.replace(initializer);
initializer.replace(nextCall);
return true;
@@ -0,0 +1,8 @@
class Foo {
void test() {
<caret>StringBuilder sb = new StringBuilder();
sb.append(sb);
sb.append("bar");
sb.append("baz");
}
}
@@ -0,0 +1,7 @@
class Foo {
void test() {
StringBuilder sb = new StringBuilder();<caret>sb.append(sb);
sb.append("bar");
sb.append("baz");
}
}
@@ -44,6 +44,7 @@ public class JoinLinesTest extends LightCodeInsightTestCase {
public void testCallChain2() { doTest(); }
public void testCallChainWrong() { doTest(); }
public void testDeclarationAndCall() { doTest(); }
public void testDeclarationAndCallSelfRef() { doTest(); }
public void testAssignmentAndCall() { doTest(); }
public void testSCR3493() {