Merge remote-tracking branch 'remotes/origin/pdolgov/moveReturnToComputation'

This commit is contained in:
Pavel Dolgov
2016-09-07 17:21:40 +03:00
118 changed files with 2453 additions and 0 deletions
@@ -0,0 +1,624 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInspection.intermediaryVariable;
import com.intellij.codeInspection.*;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.controlFlow.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.refactoring.util.RefactoringUtil;
import com.siyeh.ig.psiutils.ExpressionUtils;
import gnu.trove.THashMap;
import gnu.trove.THashSet;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
/**
* @author Pavel.Dolgov
*/
public class ReturnSeparatedFromComputationInspection extends BaseJavaBatchLocalInspectionTool {
private static final Logger LOG = Logger.getInstance("#" + ReturnSeparatedFromComputationInspection.class.getName());
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
return new JavaElementVisitor() {
@Override
public void visitReturnStatement(PsiReturnStatement returnStatement) {
super.visitReturnStatement(returnStatement);
final ReturnContext context = createReturnContext(returnStatement);
if (context != null && isApplicable(context)) {
registerProblem(holder, returnStatement, context.returnedVariable);
}
}
};
}
private static ReturnContext createReturnContext(PsiReturnStatement returnStatement) {
final PsiElement returnParent = returnStatement.getParent();
if (returnParent instanceof PsiCodeBlock) {
final PsiCodeBlock returnScope = (PsiCodeBlock)returnParent;
final PsiStatement[] statements = returnScope.getStatements();
if (statements.length != 0 && statements[statements.length - 1] == returnStatement) {
final PsiType returnType = getReturnType(returnScope);
if (returnType != null) {
PsiStatement refactoredStatement = getPrevNonEmptyStatement(returnStatement, null);
if (refactoredStatement != null) {
final PsiExpression returnValue = returnStatement.getReturnValue();
if (returnValue instanceof PsiReferenceExpression) {
final PsiElement resolved = ((PsiReferenceExpression)returnValue).resolve();
if (resolved instanceof PsiVariable) {
final PsiVariable returnedVariable = (PsiVariable)resolved;
final PsiCodeBlock variableScope = getVariableScopeBlock(returnedVariable);
if (variableScope != null) {
return new ReturnContext(returnStatement, returnScope, returnType, refactoredStatement, returnedVariable, variableScope);
}
}
}
}
}
}
}
return null;
}
@Nullable
private static PsiType getReturnType(PsiCodeBlock returnScope) {
NavigatablePsiElement returnFrom = PsiTreeUtil.getNonStrictParentOfType(returnScope, PsiMethod.class, PsiLambdaExpression.class);
if (returnFrom instanceof PsiMethod) {
return ((PsiMethod)returnFrom).getReturnType();
}
if (returnFrom instanceof PsiLambdaExpression) {
return getNonParametrizedReturnType((PsiLambdaExpression)returnFrom);
}
return null;
}
@Nullable
private static PsiType getNonParametrizedReturnType(PsiLambdaExpression lambdaExpression) {
final PsiMethod interfaceMethod = LambdaUtil.getFunctionalInterfaceMethod(lambdaExpression.getFunctionalInterfaceType());
if (interfaceMethod != null) {
final PsiType returnType = interfaceMethod.getReturnType();
if (returnType instanceof PsiPrimitiveType ||
returnType instanceof PsiClassType && ((PsiClassType)returnType).getParameterCount() == 0) {
return returnType;
}
}
return null;
}
@Nullable
private static PsiCodeBlock getVariableScopeBlock(@Nullable PsiVariable variable) {
if (variable instanceof PsiLocalVariable) {
final PsiElement variableScope = RefactoringUtil.getVariableScope((PsiLocalVariable)variable);
if (variableScope instanceof PsiCodeBlock) {
return (PsiCodeBlock)variableScope;
}
}
else if (variable instanceof PsiParameter) {
final PsiParameter parameter = (PsiParameter)variable;
final PsiElement parameterScope = parameter.getDeclarationScope();
if (parameterScope instanceof PsiMethod) {
return ((PsiMethod)parameterScope).getBody();
}
else if (parameterScope instanceof PsiLambdaExpression) {
final PsiElement lambdaBody = ((PsiLambdaExpression)parameterScope).getBody();
if (lambdaBody instanceof PsiCodeBlock) {
return (PsiCodeBlock)lambdaBody;
}
}
}
return null;
}
/**
* Detect the case like the following:
* <pre>
* int result = size;
* result = 31 * result + width;
* result = 31 * result + height;
* return result;
* </pre>
*/
private static boolean hasChainedAssignmentsInScope(@NotNull ControlFlow flow,
@NotNull PsiVariable variable,
@NotNull PsiStatement lastStatementInScope) {
for (PsiStatement statement = getPrevNonEmptyStatement(lastStatementInScope, null);
statement != null;
statement = getPrevNonEmptyStatement(statement, null)) {
if (statement instanceof PsiExpressionStatement) {
PsiExpressionStatement expressionStatement = (PsiExpressionStatement)statement;
PsiExpression expression = expressionStatement.getExpression();
if (expression instanceof PsiAssignmentExpression) {
PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression)expression;
if (isVariableUsed(flow, assignmentExpression.getLExpression(), variable) &&
isVariableUsed(flow, assignmentExpression.getRExpression(), variable)) {
return true;
}
}
}
}
return false;
}
private static boolean isVariableUsed(@NotNull ControlFlow flow, @Nullable PsiElement element, @NotNull PsiVariable variable) {
if (element == null) return false;
int startOffset = flow.getStartOffset(element);
int endOffset = flow.getEndOffset(element);
if (startOffset < 0 || endOffset < 0) return true;
return ControlFlowUtil.isVariableUsed(flow, startOffset, endOffset, variable);
}
private static boolean isApplicable(@NotNull ReturnContext context) {
final ControlFlow flow = createControlFlow(context);
return flow != null && isApplicable(flow, context);
}
@Nullable
private static ControlFlow createControlFlow(@NotNull ReturnContext context) {
try {
final ControlFlowPolicy policy = new LocalsControlFlowPolicy(context.variableScope);
return ControlFlowFactory.getInstance(context.variableScope.getProject()).getControlFlow(context.variableScope, policy);
}
catch (AnalysisCanceledException e) {
return null;
}
}
private static boolean isApplicable(@NotNull ControlFlow flow, @NotNull ReturnContext context) {
final int flowStart = flow.getStartOffset(context.returnScope);
final int flowEnd = flow.getEndOffset(context.returnScope);
if (flowStart < 0 || flowEnd < 0) return false;
final int returnStartOffset = flow.getStartOffset(context.returnStatement);
final int returnEndOffset = flow.getEndOffset(context.returnStatement);
if (returnStartOffset < 0 || returnEndOffset < 0) return false;
if (hasChainedAssignmentsInScope(flow, context.returnedVariable, context.returnStatement)) {
return false;
}
if (context.returnScope != context.variableScope &&
ControlFlowUtil.hasObservableThrowExitPoints(flow, flowStart, flowEnd,
new PsiElement[]{context.refactoredStatement}, context.variableScope)) {
return false;
}
Mover mover = new Mover(flow, context.refactoredStatement, context.returnedVariable, context.returnType, true);
mover.moveTo(context.refactoredStatement, true);
return !mover.isEmpty();
}
private static void doApply(PsiReturnStatement returnStatement) {
ReturnContext context = createReturnContext(returnStatement);
if (context != null) {
ControlFlow flow = createControlFlow(context);
if (flow != null) {
Mover mover = new Mover(flow, context.refactoredStatement, context.returnedVariable, context.returnType, false);
boolean removeReturn = mover.moveTo(context.refactoredStatement, true);
if (!mover.isEmpty()) {
applyChanges(mover, context, removeReturn);
}
}
}
}
private static void applyChanges(@NotNull Mover mover, @NotNull ReturnContext context, boolean removeReturn) {
mover.insertBefore.forEach(e -> e.getParent().addBefore(context.returnStatement, e));
mover.replaceInline.forEach(e -> {
if (e instanceof PsiBreakStatement) {
replaceStatementKeepComments((PsiBreakStatement)e, context.returnStatement);
}
else if (e instanceof PsiAssignmentExpression) {
inlineAssignment((PsiAssignmentExpression)e, context.returnStatement);
}
});
mover.removeCompletely.forEach(e -> removeElementKeepComment(e));
if (removeReturn) {
removeReturn(context);
}
}
private static void removeReturn(@NotNull ReturnContext context) {
Set<PsiElement> skippedEmptyStatements = new THashSet<>();
getPrevNonEmptyStatement(context.returnStatement, skippedEmptyStatements);
skippedEmptyStatements.forEach(PsiElement::delete);
removeElementKeepComment(context.returnStatement);
}
private static void inlineAssignment(PsiAssignmentExpression assignmentExpression, PsiReturnStatement returnStatement) {
PsiElement assignmentParent = assignmentExpression.getParent();
LOG.assertTrue(assignmentParent instanceof PsiExpressionStatement, "PsiExpressionStatement");
PsiReturnStatement returnStatementCopy = (PsiReturnStatement)returnStatement.copy();
PsiExpression rExpression = assignmentExpression.getRExpression();
PsiExpression returnValue = returnStatementCopy.getReturnValue();
if (rExpression != null && returnValue != null) {
returnValue.replace(rExpression);
replaceStatementKeepComments((PsiExpressionStatement)assignmentParent, returnStatementCopy);
}
}
private static void replaceStatementKeepComments(PsiStatement replacedStatement, PsiReturnStatement returnStatement) {
List<PsiComment> keptComments = new ArrayList<>();
for (PsiElement element = replacedStatement.getFirstChild(); element != null; element = element.getNextSibling()) {
if (element instanceof PsiComment) {
keptComments.add((PsiComment)element);
}
}
if (!keptComments.isEmpty()) {
returnStatement = (PsiReturnStatement)returnStatement.copy();
PsiElement lastReturnChild = returnStatement.getLastChild();
Project project = returnStatement.getProject();
PsiParserFacade parserFacade = PsiParserFacade.SERVICE.getInstance(project);
PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory();
if (lastReturnChild instanceof PsiComment && ((PsiComment)lastReturnChild).getTokenType() == JavaTokenType.END_OF_LINE_COMMENT) {
String commentText = StringUtil.trimStart(lastReturnChild.getText(), "//");
PsiComment inlineComment = elementFactory.createCommentFromText("/* " + commentText + " */", returnStatement);
lastReturnChild = lastReturnChild.replace(inlineComment);
}
for (PsiComment comment : keptComments) {
lastReturnChild = returnStatement.addAfter(parserFacade.createWhiteSpaceFromText(" "), lastReturnChild);
lastReturnChild = returnStatement.addAfter(comment, lastReturnChild);
}
CodeStyleManager.getInstance(project).reformat(returnStatement, true);
}
replacedStatement.replace(returnStatement);
}
private static void removeElementKeepComment(PsiElement element) {
PsiComment comment = null;
for (PsiElement child = element.getLastChild(); child != null; child = child.getPrevSibling()) {
if (child instanceof PsiComment) {
comment = (PsiComment)child;
break;
}
}
if (comment != null) {
element.replace(comment);
}
else {
element.delete();
}
}
private static class Mover {
final ControlFlow flow;
final PsiStatement enclosingStatement;
final PsiVariable resultVariable;
final PsiType returnType;
final boolean checkingApplicability;
final Set<PsiElement> insertBefore = new THashSet<>();
final Set<PsiElement> replaceInline = new THashSet<>();
final Set<PsiElement> removeCompletely = new THashSet<>();
private Map<PsiStatement, Set<PsiBreakStatement>> breakStatements;
private Mover(@NotNull ControlFlow flow,
@NotNull PsiStatement enclosingStatement,
@NotNull PsiVariable resultVariable,
PsiType returnType, boolean checkingApplicability) {
this.flow = flow;
this.enclosingStatement = enclosingStatement;
this.resultVariable = resultVariable;
this.returnType = returnType;
this.checkingApplicability = checkingApplicability;
}
boolean isEmpty() {
return insertBefore.isEmpty() && replaceInline.isEmpty();
}
/**
* Returns true if the targetStatement will always exit via return/throw/etc after the transformation,
* so if the next statement is a return or a break it can be removed safely.
*/
boolean moveTo(PsiStatement targetStatement, boolean returnAtTheEnd) {
if (checkingApplicability && !isEmpty()) {
return false; // optimization
}
if (targetStatement instanceof PsiBlockStatement) {
return moveToBlock((PsiBlockStatement)targetStatement, returnAtTheEnd);
}
if (targetStatement instanceof PsiIfStatement) {
return moveToIf((PsiIfStatement)targetStatement);
}
if (targetStatement instanceof PsiForStatement) {
return moveToFor((PsiForStatement)targetStatement);
}
if (targetStatement instanceof PsiWhileStatement) {
return moveToWhile((PsiWhileStatement)targetStatement);
}
if (targetStatement instanceof PsiDoWhileStatement) {
return moveToDoWhile((PsiDoWhileStatement)targetStatement);
}
if (targetStatement instanceof PsiForeachStatement) {
return moveToForeach((PsiForeachStatement)targetStatement);
}
if (targetStatement instanceof PsiSwitchStatement) {
return moveToSwitch((PsiSwitchStatement)targetStatement);
}
if (targetStatement instanceof PsiTryStatement) {
return moveToTry((PsiTryStatement)targetStatement, returnAtTheEnd);
}
if (targetStatement instanceof PsiLabeledStatement) {
return moveToLabeled((PsiLabeledStatement)targetStatement, returnAtTheEnd);
}
if (targetStatement instanceof PsiExpressionStatement) {
return inlineExpression((PsiExpressionStatement)targetStatement);
}
if (targetStatement instanceof PsiThrowStatement ||
targetStatement instanceof PsiReturnStatement ||
targetStatement instanceof PsiBreakStatement ||
targetStatement instanceof PsiContinueStatement) {
return true;
}
return false;
}
private boolean moveToBlock(@NotNull PsiBlockStatement targetStatement, boolean returnAtTheEnd) {
return moveToBlockBody(targetStatement.getCodeBlock(), returnAtTheEnd);
}
private boolean moveToBlockBody(@NotNull PsiCodeBlock codeBlock, boolean returnAtTheEnd) {
PsiJavaToken rBrace = codeBlock.getRBrace();
if (rBrace != null) {
PsiStatement lastNonEmptyStatement = getPrevNonEmptyStatement(rBrace, removeCompletely);
if (lastNonEmptyStatement == null || hasChainedAssignmentsInScope(flow, resultVariable, lastNonEmptyStatement)) {
return false;
}
if (moveTo(lastNonEmptyStatement, returnAtTheEnd)) {
return true;
}
if (returnAtTheEnd) {
insertBefore.add(rBrace);
return true;
}
}
return false;
}
private boolean moveToIf(@NotNull PsiIfStatement targetStatement) {
PsiStatement thenBranch = targetStatement.getThenBranch();
PsiStatement elseBranch = targetStatement.getElseBranch();
boolean thenPart = thenBranch != null && moveTo(thenBranch, false);
boolean elsePart = elseBranch != null && moveTo(elseBranch, false);
return thenPart && elsePart;
}
private boolean moveToFor(@NotNull PsiForStatement targetStatement) {
moveToBreaks(targetStatement, false);
return isAlwaysTrue(targetStatement.getCondition(), true);
}
private boolean moveToDoWhile(@NotNull PsiDoWhileStatement targetStatement) {
moveToBreaks(targetStatement, false);
return isAlwaysTrue(targetStatement.getCondition(), false);
}
private boolean moveToWhile(@NotNull PsiWhileStatement targetStatement) {
moveToBreaks(targetStatement, false);
return isAlwaysTrue(targetStatement.getCondition(), false);
}
private boolean moveToForeach(@NotNull PsiForeachStatement targetStatement) {
moveToBreaks(targetStatement, false);
return false;
}
private boolean moveToSwitch(@NotNull PsiSwitchStatement targetStatement) {
moveToBreaks(targetStatement, false);
PsiCodeBlock body = targetStatement.getBody();
return body != null && moveToBlockBody(body, false) && hasDefaultSwitchLabel(body);
}
private boolean moveToTry(@NotNull PsiTryStatement targetStatement, boolean returnAtTheEnd) {
PsiCodeBlock tryBlock = targetStatement.getTryBlock();
if (tryBlock == null) {
return false;
}
PsiCodeBlock finallyBlock = targetStatement.getFinallyBlock();
if (finallyBlock != null && isVariableUsed(flow, finallyBlock, resultVariable)) {
return false;
}
boolean allCatchesReturn = true;
PsiCatchSection[] catchSections = targetStatement.getCatchSections();
for (PsiCatchSection catchSection : catchSections) {
PsiCodeBlock catchBlock = catchSection.getCatchBlock();
if (catchBlock == null || !moveToBlockBody(catchBlock, false)) {
allCatchesReturn = false;
}
}
return moveToBlockBody(tryBlock, returnAtTheEnd && allCatchesReturn) && allCatchesReturn;
}
private boolean moveToLabeled(@NotNull PsiLabeledStatement targetStatement, boolean returnAtTheEnd) {
PsiStatement statement = targetStatement.getStatement();
if (statement == null) {
return false;
}
moveToBreaks(statement, false);
return moveTo(statement, returnAtTheEnd);
}
private boolean inlineExpression(@NotNull PsiExpressionStatement statement) {
PsiExpression expression = statement.getExpression();
if (expression instanceof PsiAssignmentExpression) {
PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression)expression;
PsiExpression lExpression = assignmentExpression.getLExpression();
if (assignmentExpression.getOperationTokenType() == JavaTokenType.EQ && isReferenceTo(lExpression, resultVariable)) {
PsiExpression rExpression = assignmentExpression.getRExpression();
if (rExpression != null) {
PsiType rExpressionType = rExpression.getType();
if (rExpressionType != null && returnType.isAssignableFrom(rExpressionType)) {
replaceInline.add(assignmentExpression);
return true;
}
}
}
}
return false;
}
private void moveToBreaks(@NotNull PsiStatement targetStatement, boolean returnAtTheEnd) {
Set<PsiBreakStatement> breaks = getBreaks(targetStatement);
for (PsiBreakStatement breakStatement : breaks) {
PsiStatement prevNonEmptyStatement = getPrevNonEmptyStatement(breakStatement, removeCompletely);
if (prevNonEmptyStatement == null || !moveTo(prevNonEmptyStatement, returnAtTheEnd)) {
replaceInline.add(breakStatement);
}
else {
removeCompletely.add(breakStatement);
}
}
}
private static boolean hasDefaultSwitchLabel(@NotNull PsiCodeBlock switchBody) {
for (PsiStatement statement : switchBody.getStatements()) {
if (statement instanceof PsiSwitchLabelStatement && ((PsiSwitchLabelStatement)statement).isDefaultCase()) {
return true;
}
}
return false;
}
private static boolean isAlwaysTrue(@Nullable PsiExpression condition, boolean nullIsTrue) {
if(condition == null) return nullIsTrue;
return ExpressionUtils.computeConstantExpression(condition) == Boolean.TRUE;
}
private Set<PsiBreakStatement> getBreaks(@NotNull PsiStatement targetStatement) {
if (breakStatements == null) {
breakStatements = new THashMap<>();
List<Instruction> instructions = flow.getInstructions();
for (int i = 0; i < instructions.size(); i++) {
PsiElement element = flow.getElement(i);
PsiStatement statement = getNearestEnclosingStatement(element);
if (statement instanceof PsiBreakStatement) {
PsiStatement exitedStatement = ((PsiBreakStatement)statement).findExitedStatement();
if (exitedStatement != null) {
breakStatements.computeIfAbsent(exitedStatement, unused -> new THashSet<>()).add((PsiBreakStatement)statement);
}
}
}
}
Set<PsiBreakStatement> breaks = breakStatements.get(targetStatement);
return breaks != null ? breaks : Collections.emptySet();
}
private static boolean isReferenceTo(PsiExpression expression, PsiVariable variable) {
if (expression instanceof PsiReferenceExpression) {
PsiReferenceExpression referenceExpression = (PsiReferenceExpression)expression;
if (!referenceExpression.isQualified() && referenceExpression.resolve() == variable) {
return true;
}
}
return false;
}
}
@Nullable
private static PsiStatement getPrevNonEmptyStatement(@Nullable PsiElement psiElement, @Nullable Set<PsiElement> skippedEmptyStatements) {
if (psiElement == null || !(psiElement.getParent() instanceof PsiCodeBlock)) {
return null;
}
PsiStatement prevStatement = PsiTreeUtil.getPrevSiblingOfType(psiElement, PsiStatement.class);
List<PsiStatement> skipped = new ArrayList<>();
while (prevStatement instanceof PsiEmptyStatement) {
skipped.add(prevStatement);
prevStatement = PsiTreeUtil.getPrevSiblingOfType(prevStatement, PsiStatement.class);
}
if (prevStatement != null && skippedEmptyStatements != null) {
skippedEmptyStatements.addAll(skipped);
}
return prevStatement;
}
@Nullable
private static PsiStatement getNearestEnclosingStatement(@Nullable PsiElement element) {
return element instanceof PsiStatement ? (PsiStatement)element : PsiTreeUtil.getParentOfType(element, PsiStatement.class);
}
private static void registerProblem(@NotNull ProblemsHolder holder,
@NotNull PsiReturnStatement returnStatement,
@NotNull PsiVariable variable) {
String name = variable.getName();
holder.registerProblem(returnStatement,
InspectionsBundle.message("inspection.return.separated.from.computation.descriptor", name),
new VariableFix(name));
}
private static class VariableFix implements LocalQuickFix {
private String myName;
public VariableFix(String name) {
myName = name;
}
@Nls
@NotNull
@Override
public String getName() {
return InspectionsBundle.message("inspection.return.separated.from.computation.quickfix", myName);
}
@Nls
@NotNull
@Override
public String getFamilyName() {
return InspectionsBundle.message("inspection.return.separated.from.computation.family.quickfix");
}
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement element = descriptor.getPsiElement();
if (element instanceof PsiReturnStatement) {
doApply(((PsiReturnStatement)element));
}
}
}
private static class ReturnContext {
final PsiReturnStatement returnStatement;
final PsiCodeBlock returnScope;
final PsiType returnType;
final PsiStatement refactoredStatement;
final PsiVariable returnedVariable;
final PsiCodeBlock variableScope;
private ReturnContext(@NotNull PsiReturnStatement returnStatement,
@NotNull PsiCodeBlock returnScope,
@NotNull PsiType returnType,
@NotNull PsiStatement refactoredStatement,
@NotNull PsiVariable returnedVariable,
@NotNull PsiCodeBlock variableScope) {
this.returnStatement = returnStatement;
this.returnScope = returnScope;
this.returnType = returnType;
this.refactoredStatement = refactoredStatement;
this.returnedVariable = returnedVariable;
this.variableScope = variableScope;
}
}
}
@@ -238,6 +238,26 @@ public class ControlFlowUtil {
return array;
}
public static boolean isVariableUsed(ControlFlow flow, int start, int end, PsiVariable variable) {
List<Instruction> instructions = flow.getInstructions();
LOG.assertTrue(start >= 0, "flow start");
LOG.assertTrue(end <= instructions.size(), "flow end");
for (int i = start; i < end; i++) {
Instruction instruction = instructions.get(i);
if (instruction instanceof ReadVariableInstruction) {
if (((ReadVariableInstruction)instruction).variable == variable) {
return true;
}
}
else if (instruction instanceof WriteVariableInstruction) {
if (((WriteVariableInstruction)instruction).variable == variable) {
return true;
}
}
}
return false;
}
public static List<PsiVariable> getInputVariables(ControlFlow flow, int start, int end) {
List<PsiVariable> usedVariables = getUsedVariables(flow, start, end);
ArrayList<PsiVariable> array = new ArrayList<PsiVariable>(usedVariables.size());
@@ -0,0 +1,19 @@
// "Move 'return' closer to computation of the value of 's'" "true"
import java.io.*;
class T {
private static String getString() throws IOException {
String s;
try (BufferedReader reader = open()) {
while (true) {
s = reader.readLine();
if (s == null || s.startsWith("$")) {
return s;
}
}
}
}
private static BufferedReader open() throws FileNotFoundException {
return null;
}
}
@@ -0,0 +1,21 @@
// "Move 'return' closer to computation of the value of 'r'" "true"
class T {
String f() {
String r = "";
do {
if (!hasNext()) return r;
String s = next();
if (s != null) {
return s;
}
} while (true);
}
boolean hasNext() {
return true;
}
String next() {
return null;
}
}
@@ -0,0 +1,17 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int a[]) {
int n = 0;
for (int i=0; i<a.length; i++) {
if (n < a[i]) n = a[i];
if (n > 100) {
return n; // at the end 1
}
if (n < 0) {
return 0; // at the end 2
/* inline */
}
}
return n;
}
}
@@ -0,0 +1,11 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f() {
int n = -1;
for(int i=0;; i++) {
if (i % 127 == 0 && i % 129 == 0) {
return i + 1;
}
}
}
}
@@ -0,0 +1,16 @@
// "Move 'return' closer to computation of the value of 'raw'" "true"
import java.util.*;
class T {
List<String> f(boolean b) {
List raw = null;
if (b) {
return g();
}
return raw;
}
List<String> g() {
return Collections.singletonList("");
}
}
@@ -0,0 +1,16 @@
// "Move 'return' closer to computation of the value of 's'" "true"
class T {
String f(String a) {
String s = a;
if (s == null) {
return ""; /* return comment */ // end of line
}
else if (s.startsWith("@")) {
return s.substring(1); // return comment
}
else if (s.startsWith("#")) {
return "#"; /* return comment */ /* inline */
}
return s; // return comment
}
}
@@ -0,0 +1,12 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = -1;
if (b) {
return 1;
}
else {
throw new RuntimeException();
}
}
}
@@ -0,0 +1,8 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = 0;
if (b) return 1;
else return 2;
}
}
@@ -0,0 +1,9 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = 0;
if (b) System.out.println("yes");
else return 2;
return n;
}
}
@@ -0,0 +1,9 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = 0;
if (b) return 1;
else System.out.println("no");
return n;
}
}
@@ -0,0 +1,12 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = -1;
if (b) {
throw new RuntimeException();
}
else {
return 2;
}
}
}
@@ -0,0 +1,26 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean b, boolean c) {
int n = -1;
if (b) {
try {
return g();
}
catch (RuntimeException e) {
d(e);
}
}
else {
return 2;
}
return n;
}
int g() {
return 1;
}
void d(Exception e) {
e.printStackTrace()
}
}
@@ -0,0 +1,12 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n;
myLabel:
{
n = 1;
if (b) return n;
return 2;
}
}
}
@@ -0,0 +1,13 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int[] a) {
int n = -1;
myLabel:
for (int i = 0; i < a.length; i++) {
if (a[0] == 0) {
return i;
}
}
return n;
}
}
@@ -0,0 +1,12 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int[] a) {
int n = -1;
myLabel:
for (int i = 0; i < a.length; i++) {
n = i;
if (a[0] == 0) return n;
}
return n;
}
}
@@ -0,0 +1,15 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int[][] a) {
int n = -1;
myLabel:
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (a[i][j] == 0) {
return j;
}
}
}
return n;
}
}
@@ -0,0 +1,17 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int[][] a) {
int n = -1;
myLabel:
for (int i = 0; i < a.length; i++) {
if (a[i].length == 0) {
return -i - 1;
}
for(int j = 0; j < a[i].length; j++) {
n = j;
if (a[i][j] == 0) return n;
}
}
return n;
}
}
@@ -0,0 +1,9 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = 0;
myLabel:
if (b) return 1;
else return n;
}
}
@@ -0,0 +1,9 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f() {
int n;
{
return 1;
}
}
}
@@ -0,0 +1,11 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f() {
int n;
{
n = 1;
System.out.println();
return n;
}
}
}
@@ -0,0 +1,12 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean a, boolean b) {
int n = -1;
if (a) {
if (b) {
return 1;
}
}
return n;
}
}
@@ -0,0 +1,11 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean a, boolean b) {
int n = -1;
if (a) {
if (b) return 1;
else return 2;
}
return n;
}
}
@@ -0,0 +1,11 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean a, boolean b) {
int n = -1;
if (a) {
if (b) return 1;
}
else return 2;
return n;
}
}
@@ -0,0 +1,15 @@
// "Move 'return' closer to computation of the value of 's'" "true"
import java.io.*;
class T {
private static String getString() throws IOException {
String s;
try (BufferedReader r = open()) {
return r.readLine();
}
}
private static BufferedReader open() throws FileNotFoundException {
return null;
}
}
@@ -0,0 +1,25 @@
// "Move 'return' closer to computation of the value of 'r'" "true"
class T {
String f(String a) {
String r = "";
int i = 0;
do {
int j = a.indexOf(",", i);
String s = j > i ? a.substring(i, j) : a.substring(i);
if (s.startsWith("@")) {
return s;
}
i = j + 1;
}
while (i >= 0);
return r;
}
boolean hasNext() {
return true;
}
String next() {
return null;
}
}
@@ -0,0 +1,12 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int[] a, int b) {
int n = -1;
for (int i = 0; i < a.length; i++) {
if (a[i] == b) {
return i;
}
}
return n;
}
}
@@ -0,0 +1,12 @@
// "Move 'return' closer to computation of the value of 'r'" "true"
class T {
String f(String[] a) {
String r = "";
for (String s : a) {
if (s != null && s.contains("@")) {
return s + ":" + s.length();
}
}
return r;
}
}
@@ -0,0 +1,8 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = 0;
if (b) return 1;
return n;
}
}
@@ -0,0 +1,11 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = g();
if (b) return 1;
return n;
}
int g() {
return 0;
}
}
@@ -0,0 +1,8 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean b, int d) {
int n = d;
if (b) return 1;
return n;
}
}
@@ -0,0 +1,21 @@
// "Move 'return' closer to computation of the value of 'r'" "true"
class T {
String f() {
String r = "";
while (hasNext()) {
String s = next();
if (s != null) {
return s;
}
}
return r;
}
boolean hasNext() {
return true;
}
String next() {
return null;
}
}
@@ -0,0 +1,13 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int a) {
int n = -1;
switch (a) {
case 1:
return 2;
case 2:
return 4;
}
return n;
}
}
@@ -0,0 +1,13 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int a) {
int n = -1;
switch (a) {
case 1:
return 2;
case 2:
return 4;
}
return n;
}
}
@@ -0,0 +1,14 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int a) {
int n = -1;
switch (a) {
case 1:
return 2;
case 2:
return 4;
default:
return 0;
}
}
}
@@ -0,0 +1,16 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int a) {
int n = -1;
switch (a) {
case 1:
return 2;
case 2:
return 4;
default:
return 0;
case 0:
}
return n;
}
}
@@ -0,0 +1,14 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int a) {
int n = -1;
switch (a) {
case 1:
return 2;
default:
return 0;
case 2:
return 4;
}
}
}
@@ -0,0 +1,16 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = -1;
try {
n = 1;
if (b) {
throw new RuntimeException();
}
return n;
}
catch (RuntimeException e) {
return 2;
}
}
}
@@ -0,0 +1,24 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
import java.io.*;
class T {
int f(boolean b, boolean c) {
int n = -1;
try {
n = 1;
if (b) throw new IOException();
n = 2;
if (c) throw new RuntimeException();
return n;
}
catch (IOException e) {
throw new RuntimeException(e);
}
catch (RuntimeException e) {
return 3;
}
finally {
System.out.println();
}
}
}
@@ -0,0 +1,24 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
import java.io.*;
class T {
int f(boolean b, boolean c) {
int n = -1;
try {
n = 1;
if (b) throw new IOException();
n = 2;
if (c) throw new RuntimeException();
return n;
}
catch (IOException e) {
throw new RuntimeException(e);
}
catch (RuntimeException e) {
return 3;
}
finally {
System.out.println();
}
}
}
@@ -0,0 +1,24 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
import java.io.*;
class T {
int f(boolean b, boolean c) {
int n = -1;
try {
n = 1;
if (b) throw new IOException();
n = 2;
if (c) throw new RuntimeException();
return n;
}
catch (IOException e) {
throw new RuntimeException(e);
}
catch (RuntimeException e) {
return 3;
}
finally {
return 4;
}
}
}
@@ -0,0 +1,24 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
import java.io.*;
class T {
int f(boolean b, boolean c, boolean d) {
int n = -1;
try {
n = 1;
if (b) throw new IOException();
n = 2;
if (c) throw new RuntimeException();
return n;
}
catch (IOException e) {
throw new RuntimeException(e);
}
catch (RuntimeException e) {
return 3;
}
finally {
if(d) return 4;
}
}
}
@@ -0,0 +1,16 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = -1;
try {
n = 1;
if (b) {
throw new RuntimeException();
}
return n;
}
catch (RuntimeException e) {
throw new RuntimeException(e);
}
}
}
@@ -0,0 +1,16 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = -1;
try {
n = 1;
if (b) {
throw new RuntimeException();
}
return n;
}
catch (RuntimeException e) {
return 2;
}
}
}
@@ -0,0 +1,23 @@
// "Move 'return' closer to computation of the value of 'r'" "true"
class T {
String f(String p) {
String r = null;
try {
while (true) {
String n = next();
if (n != null) {
String t = n.toLowerCase();
if (t.equals(p)) {
return n;
}
}
}
} finally {
System.out.println();
}
}
String next() {
return "";
}
}
@@ -0,0 +1,24 @@
// "Move 'return' closer to computation of the value of 'r'" "true"
class T {
String f(String p) {
String r = null;
try {
while (true) {
String n = next();
if (n != null) return r;
if ("@".eqals(n)) {
String t = n.toLowerCase();
if (t.equals(p)) {
return n;
}
}
}
} finally {
System.out.println();
}
}
String next() {
return "";
}
}
@@ -0,0 +1,14 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int a) {
int n = -1;
while (true) {
n = g();
if(n != 0) return n;
}
}
int g() {
return 1;
}
}
@@ -0,0 +1,15 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int a) {
int n = -1;
while (n <= 0) {
n = g();
if (n != 0) return n;
}
return n;
}
int g() {
return 1;
}
}
@@ -0,0 +1,20 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int a) {
int n = -1;
int t = a;
while (t != null) {
if (t == 1) {
return 10;
}
else if (t == 2) {
return 20;
}
else {
t = t + 1;
continue;
}
}
return n;
}
}
@@ -0,0 +1,15 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int a) {
int n = -1;
while (true) {
n = g();
if(n == 0) continue;
else return n;
}
}
int g() {
return 1;
}
}
@@ -0,0 +1,15 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int a) {
int n = -1;
while (true) {
n = g();
if(n == 0) continue;
return n;
}
}
int g() {
return 1;
}
}
@@ -0,0 +1,14 @@
// "Move 'return' closer to computation of the value of 'r'" "true"
class T {
long f() {
long r;
long s = System.currentTimeMillis();
long t = s;
while (true) {
t = System.currentTimeMillis();
if (t - s > 100) {
return t;
}
}
}
}
@@ -0,0 +1,8 @@
// "Move 'return' closer to computation of the value of 'n'" "false"
class T {
int f(int a) {
int n = a;
assert n != 0;
r<caret>eturn n;
}
}
@@ -0,0 +1,15 @@
// "Move 'return' closer to computation of the value of 'n'" "false"
class T {
int x;
int y;
int f(int a) {
int n = -1;
if (a != 0) {
n = a;
n = 31 * x + n;
n = 31 * y + n;
}
re<caret>turn n;
}
}
@@ -0,0 +1,20 @@
// "Move 'return' closer to computation of the value of 's'" "true"
import java.io.*;
class T {
private static String getString() throws IOException {
String s;
try (BufferedReader reader = open()) {
while (true) {
s = reader.readLine();
if (s == null || s.startsWith("$")) {
break;
}
}
}
r<caret>eturn s;
}
private static BufferedReader open() throws FileNotFoundException {
return null;
}
}
@@ -0,0 +1,23 @@
// "Move 'return' closer to computation of the value of 'r'" "true"
class T {
String f() {
String r = "";
do {
if (!hasNext()) break;
String s = next();
if (s != null) {
r = s;
break;
}
} while (true);
<caret>return r;
}
boolean hasNext() {
return true;
}
String next() {
return null;
}
}
@@ -0,0 +1,17 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int a[]) {
int n = 0;
for (int i=0; i<a.length; i++) {
if (n < a[i]) n = a[i];
if (n > 100) {
break; // at the end 1
}
if (n < 0) {
n = 0; // at the end 2
break; /* inline */
}
}
ret<caret>urn n;
}
}
@@ -0,0 +1,13 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f() {
int n = -1;
for(int i=0;; i++) {
if (i % 127 == 0 && i % 129 == 0) {
n = i + 1;
break;
}
}
r<caret>eturn n;
}
}
@@ -0,0 +1,16 @@
// "Move 'return' closer to computation of the value of 'raw'" "true"
import java.util.*;
class T {
List<String> f(boolean b) {
List raw = null;
if (b) {
raw = g();
}
re<caret>turn raw;
}
List<String> g() {
return Collections.singletonList("");
}
}
@@ -0,0 +1,16 @@
// "Move 'return' closer to computation of the value of 'raw'" "false"
import java.util.*;
class T {
List<Object> f(boolean b) {
List raw = null;
if (b) {
raw = g();
}
re<caret>turn raw;
}
List<String> g() {
return Collections.singletonList("");
}
}
@@ -0,0 +1,13 @@
// "Move 'return' closer to computation of the value of 'result'" "false"
class T {
int size;
int width;
int height;
public int hashCode() {
int result = size;
result = 31 * result + width;
result = 31 * result + height;
ret<caret>urn result;
}
}
@@ -0,0 +1,16 @@
// "Move 'return' closer to computation of the value of 's'" "true"
class T {
String f(String a) {
String s = a;
if (s == null) {
s = ""; // end of line
}
else if (s.startsWith("@")) {
s = s.substring(1);
}
else if (s.startsWith("#")) {
s = "#"; /* inline */
}
ret<caret>urn s; // return comment
}
}
@@ -0,0 +1,9 @@
// "Move 'return' closer to computation of the value of 'n'" "false"
class T {
int f(boolean b) {
int n = 0;
if (b) System.out.println("yes");
else System.out.println("no");
ret<caret>urn n;
}
}
@@ -0,0 +1,13 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = -1;
if (b) {
n = 1;
}
else {
throw new RuntimeException();
}
r<caret>eturn n;
}
}
@@ -0,0 +1,9 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = 0;
if (b) n = 1;
else n = 2;
r<caret>eturn n;
}
}
@@ -0,0 +1,9 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = 0;
if (b) System.out.println("yes");
else n = 2;
r<caret>eturn n;
}
}
@@ -0,0 +1,9 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = 0;
if (b) n = 1;
else System.out.println("no");
ret<caret>urn n;
}
}
@@ -0,0 +1,10 @@
// "Move 'return' closer to computation of the value of 'n'" "false"
class T {
int f(boolean b) {
int n = -1;
if (b) {
throw new RuntimeException();
}
r<caret>eturn n;
}
}
@@ -0,0 +1,13 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = -1;
if (b) {
throw new RuntimeException();
}
else {
n = 2;
}
r<caret>eturn n;
}
}
@@ -0,0 +1,26 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean b, boolean c) {
int n = -1;
if (b) {
try {
n = g();
}
catch (RuntimeException e) {
d(e);
}
}
else {
n = 2;
}
r<caret>eturn n;
}
int g() {
return 1;
}
void d(Exception e) {
e.printStackTrace()
}
}
@@ -0,0 +1,13 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n;
myLabel:
{
n = 1;
if (b) break myLabel;
n = 2;
}
ret<caret>urn n;
}
}
@@ -0,0 +1,14 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int[] a) {
int n = -1;
myLabel:
for (int i = 0; i < a.length; i++) {
if (a[0] == 0) {
n = i;
break myLabel;
}
}
re<caret>turn n;
}
}
@@ -0,0 +1,12 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int[] a) {
int n = -1;
myLabel:
for (int i = 0; i < a.length; i++) {
n = i;
if (a[0] == 0) break myLabel;
}
re<caret>turn n;
}
}
@@ -0,0 +1,16 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int[][] a) {
int n = -1;
myLabel:
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (a[i][j] == 0) {
n = j;
break myLabel;
}
}
}
re<caret>turn n;
}
}
@@ -0,0 +1,18 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int[][] a) {
int n = -1;
myLabel:
for (int i = 0; i < a.length; i++) {
if (a[i].length == 0) {
n = -i - 1;
break myLabel;
}
for(int j = 0; j < a[i].length; j++) {
n = j;
if (a[i][j] == 0) break myLabel;
}
}
re<caret>turn n;
}
}
@@ -0,0 +1,10 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = 0;
myLabel:
if (b) n = 1;
else break myLabel;
ret<caret>urn n;
}
}
@@ -0,0 +1,10 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f() {
int n;
{
n = 1;
}
ret<caret>urn n;
}
}
@@ -0,0 +1,11 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f() {
int n;
{
n = 1;
System.out.println();
}
re<caret>turn n;
}
}
@@ -0,0 +1,12 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean a, boolean b) {
int n = -1;
if (a) {
if (b) {
n = 1;
}
}
ret<caret>urn n;
}
}
@@ -0,0 +1,11 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean a, boolean b) {
int n = -1;
if (a) {
if (b) n = 1;
else n = 2;
}
r<caret>eturn n;
}
}
@@ -0,0 +1,11 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean a, boolean b) {
int n = -1;
if (a) {
if (b) n = 1;
}
else n = 2;
re<caret>turn n;
}
}
@@ -0,0 +1,16 @@
// "Move 'return' closer to computation of the value of 's'" "true"
import java.io.*;
class T {
private static String getString() throws IOException {
String s;
try (BufferedReader r = open()) {
s = r.readLine();
}
re<caret>turn s;
}
private static BufferedReader open() throws FileNotFoundException {
return null;
}
}
@@ -0,0 +1,10 @@
// "Move 'return' closer to computation of the value of 'r'" "false"
class T {
int[] f(boolean b) {
int[] r = new int[]{-1};
if (b) {
r[0] = 1;
}
re<caret>turn r;
}
}
@@ -0,0 +1,26 @@
// "Move 'return' closer to computation of the value of 'r'" "true"
class T {
String f(String a) {
String r = "";
int i = 0;
do {
int j = a.indexOf(",", i);
String s = j > i ? a.substring(i, j) : a.substring(i);
if (s.startsWith("@")) {
r = s;
break;
}
i = j + 1;
}
while (i >= 0);
retu<caret>rn r;
}
boolean hasNext() {
return true;
}
String next() {
return null;
}
}
@@ -0,0 +1,13 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int[] a, int b) {
int n = -1;
for (int i = 0; i < a.length; i++) {
if (a[i] == b) {
n = i;
break;
}
}
r<caret>eturn n;
}
}
@@ -0,0 +1,13 @@
// "Move 'return' closer to computation of the value of 'r'" "true"
class T {
String f(String[] a) {
String r = "";
for (String s : a) {
if (s != null && s.contains("@")) {
r = s + ":" + s.length();
break;
}
}
r<caret>eturn r;
}
}
@@ -0,0 +1,8 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = 0;
if (b) n = 1;
re<caret>turn n;
}
}
@@ -0,0 +1,11 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = g();
if (b) n = 1;
re<caret>turn n;
}
int g() {
return 0;
}
}
@@ -0,0 +1,8 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean b, int d) {
int n = d;
if (b) n = 1;
re<caret>turn n;
}
}
@@ -0,0 +1,22 @@
// "Move 'return' closer to computation of the value of 'r'" "true"
class T {
String f() {
String r = "";
while (hasNext()) {
String s = next();
if (s != null) {
r = s;
break;
}
}
re<caret>turn r;
}
boolean hasNext() {
return true;
}
String next() {
return null;
}
}
@@ -0,0 +1,15 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int a) {
int n = -1;
switch (a) {
case 1:
n = 2;
break;
case 2:
n = 4;
break;
}
r<caret>eturn n;
}
}
@@ -0,0 +1,14 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int a) {
int n = -1;
switch (a) {
case 1:
n = 2;
break;
case 2:
n = 4;
}
ret<caret>urn n;
}
}
@@ -0,0 +1,17 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int a) {
int n = -1;
switch (a) {
case 1:
n = 2;
break;
case 2:
n = 4;
break;
default:
n = 0;
}
ret<caret>urn n;
}
}
@@ -0,0 +1,19 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int a) {
int n = -1;
switch (a) {
case 1:
n = 2;
break;
case 2:
n = 4;
break;
default:
n = 0;
break;
case 0:
}
re<caret>turn n;
}
}
@@ -0,0 +1,17 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(int a) {
int n = -1;
switch (a) {
case 1:
n = 2;
break;
default:
n = 0;
break;
case 2:
n = 4;
}
ret<caret>urn n;
}
}
@@ -0,0 +1,16 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
class T {
int f(boolean b) {
int n = -1;
try {
n = 1;
if (b) {
throw new RuntimeException();
}
}
catch (RuntimeException e) {
n = 2;
}
re<caret>turn n;
}
}
@@ -0,0 +1,24 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
import java.io.*;
class T {
int f(boolean b, boolean c) {
int n = -1;
try {
n = 1;
if (b) throw new IOException();
n = 2;
if (c) throw new RuntimeException();
}
catch (IOException e) {
throw new RuntimeException(e);
}
catch (RuntimeException e) {
return 3;
}
finally {
System.out.println();
}
re<caret>turn n;
}
}
@@ -0,0 +1,24 @@
// "Move 'return' closer to computation of the value of 'n'" "true"
import java.io.*;
class T {
int f(boolean b, boolean c) {
int n = -1;
try {
n = 1;
if (b) throw new IOException();
n = 2;
if (c) throw new RuntimeException();
}
catch (IOException e) {
throw new RuntimeException(e);
}
catch (RuntimeException e) {
n = 3;
}
finally {
System.out.println();
}
re<caret>turn n;
}
}
@@ -0,0 +1,24 @@
// "Move 'return' closer to computation of the value of 'n'" "false"
import java.io.*;
class T {
int f(boolean b, boolean c) {
int n = -1;
try {
n = 1;
if (b) throw new IOException();
n = 2;
if (c) throw new RuntimeException();
}
catch (IOException e) {
throw new RuntimeException(e);
}
catch (RuntimeException e) {
n = 3;
}
finally {
n = 4;
}
re<caret>turn n;
}
}

Some files were not shown because too many files have changed in this diff Show More