mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Java: Check that the variable used in conditional return isn't used anywhere else (IDEA-152688)
This commit is contained in:
@@ -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 {}
|
||||
|
||||
+4
-1
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user