mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Java: Use InlineUtil for removing redundant variable (IDEA-182669)
This commit is contained in:
+6
-4
@@ -11,10 +11,12 @@ import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiTypesUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.refactoring.util.InlineUtil;
|
||||
import com.intellij.refactoring.util.RefactoringUtil;
|
||||
import com.intellij.util.Query;
|
||||
import com.siyeh.ig.psiutils.CommentTracker;
|
||||
import com.siyeh.ig.psiutils.ExpressionUtils;
|
||||
import com.siyeh.ig.psiutils.HighlightUtils;
|
||||
import com.siyeh.ig.psiutils.SideEffectChecker;
|
||||
import gnu.trove.THashMap;
|
||||
import gnu.trove.THashSet;
|
||||
@@ -34,9 +36,6 @@ public class ReturnSeparatedFromComputationInspection extends AbstractBaseJavaLo
|
||||
@NotNull
|
||||
@Override
|
||||
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
|
||||
if (!(holder.getFile() instanceof PsiJavaFile)) {
|
||||
return PsiElementVisitor.EMPTY_VISITOR;
|
||||
}
|
||||
return new JavaElementVisitor() {
|
||||
@Override
|
||||
public void visitReturnStatement(PsiReturnStatement returnStatement) {
|
||||
@@ -218,15 +217,18 @@ public class ReturnSeparatedFromComputationInspection extends AbstractBaseJavaLo
|
||||
return;
|
||||
}
|
||||
}
|
||||
List<PsiExpression> inlinedExpressions = new ArrayList<>();
|
||||
boolean isSingleUsage = value != null && usages.size() == 1;
|
||||
if (isSimple || isSingleUsage) {
|
||||
for (PsiReference usage : usages) {
|
||||
usage.getElement().replace(value);
|
||||
PsiExpression expression = InlineUtil.inlineVariable(context.returnedVariable, value, (PsiJavaCodeReferenceElement)usage);
|
||||
inlinedExpressions.add(expression);
|
||||
}
|
||||
}
|
||||
if (isSimple || isSingleUsage || usages.isEmpty()) {
|
||||
context.returnedVariable.delete();
|
||||
}
|
||||
HighlightUtils.highlightElements(inlinedExpressions);
|
||||
}
|
||||
|
||||
@Contract("null -> false")
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// "Move 'return' closer to computation of the value of 'n'" "true"
|
||||
class T {
|
||||
int[] f(int k) {
|
||||
if (k == 1)
|
||||
return new int[]{1};
|
||||
return new int[]{-1};
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// "Move 'return' closer to computation of the value of 'n'" "true"
|
||||
class T {
|
||||
int[] f(int k) {
|
||||
int[] n = {-1};
|
||||
if (k == 1)
|
||||
n = new int[]{1};
|
||||
<caret>return n;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user