Java: Check that the variable used in conditional return isn't used anywhere else (IDEA-152688)

This commit is contained in:
Pavel Dolgov
2017-06-15 13:09:00 +03:00
parent 9c86885ea0
commit 509fa38e40
3 changed files with 7 additions and 17 deletions
@@ -147,8 +147,8 @@ public class ControlFlowWrapper {
return myExitStatements;
}
public List<PsiVariable> filterUsedVariables(PsiVariable[] outputVariables) {
return ControlFlowUtil.filterUsedVariables(myControlFlow, myFlowEnd, outputVariables);
public boolean isVariableUsedAfterEnd(PsiVariable variable) {
return ControlFlowUtil.needVariableValueAt(variable, myControlFlow, myFlowEnd);
}
public static class ExitStatementsNotSameException extends Exception {}
@@ -874,7 +874,10 @@ public class ExtractMethodObjectProcessor extends BaseRefactoringProcessor {
setMethodCall((PsiMethodCallExpression)((PsiLocalVariable)replace.getDeclaredElements()[0]).getInitializer());
}
final List<PsiVariable> usedVariables = myControlFlowWrapper.filterUsedVariables(myOutputVariables);
PsiVariable[] usedVariables = myOutputVariables;
if (generatesConditionalExit() && myOutputVariable != null && !myControlFlowWrapper.isVariableUsedAfterEnd(myOutputVariable)) {
usedVariables = ArrayUtil.remove(usedVariables, myOutputVariable);
}
Collection<ControlFlowUtil.VariableInfo> reassigned = myControlFlowWrapper.getInitializedTwice();
for (PsiVariable variable : usedVariables) {
String name = variable.getName();
@@ -148,7 +148,7 @@ public class ControlFlowUtil {
return result;
}
private static boolean needVariableValueAt(final PsiVariable variable, final ControlFlow flow, final int offset) {
public static boolean needVariableValueAt(final PsiVariable variable, final ControlFlow flow, final int offset) {
InstructionClientVisitor<Boolean> visitor = new InstructionClientVisitor<Boolean>() {
final boolean[] neededBelow = new boolean[flow.getSize() + 1];
@@ -361,19 +361,6 @@ public class ControlFlowUtil {
return outputVariables;
}
public static List<PsiVariable> filterUsedVariables(ControlFlow flow, int offset, PsiVariable[] variables) {
if (offset >= flow.getSize()) {
return Collections.emptyList();
}
List<PsiVariable> result = new ArrayList<>();
for (PsiVariable variable : variables) {
if (needVariableValueAt(variable, flow, offset)) {
result.add(variable);
}
}
return result;
}
public static Collection<PsiStatement> findExitPointsAndStatements(final ControlFlow flow, final int start, final int end, final IntArrayList exitPoints,
final Class... classesFilter) {
if (end == start) {