private boolean isCallResultUsed(PsiMethodCallExpression call) { if (hasChainedMethodCall(call)) { return true; } else if (getBoundVariable(call, true) != null) { // TODO: Check that variable is actually used return true; } else if (PsiTreeUtil.getParentOfType(call, PsiReturnStatement.class) != null) { return true; } else if (PsiTreeUtil.getParentOfType(call, PsiMethodCallExpression.class) != null) { return true; } return false; }
@Override public void visitReturnStatement(@NotNull PsiReturnStatement statement) { super.visitReturnStatement(statement); if (!ControlFlowUtils.isInFinallyBlock(statement)) { return; } registerStatementError(statement); }
@Override public void visitReturnStatement(@NotNull PsiReturnStatement returnStatement) { if (returned) { return; } super.visitReturnStatement(returnStatement); final PsiExpression returnValue = returnStatement.getReturnValue(); if (VariableAccessUtils.mayEvaluateToVariable(returnValue, variable, myBuilderPattern)) { returned = true; } }
protected static void addStatementBefore(String newStatementText, PsiReturnStatement anchor) { final Project project = anchor.getProject(); final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); final PsiStatement newStatement = factory.createStatementFromText(newStatementText, anchor); final PsiElement addedStatement = anchor.getParent().addBefore(newStatement, anchor); CodeStyleManager.getInstance(project).reformat(addedStatement); }
private void checkIfStatementWithoutElseBranch( PsiIfStatement ifStatement) { final PsiStatement thenBranch = ifStatement.getThenBranch(); if (ControlFlowUtils.statementMayCompleteNormally(thenBranch)) { return; } PsiStatement nextStatement = getNextStatement(ifStatement); if (thenBranch instanceof PsiBlockStatement) { final PsiBlockStatement blockStatement = (PsiBlockStatement)thenBranch; final PsiCodeBlock codeBlock = blockStatement.getCodeBlock(); final PsiStatement[] statements = codeBlock.getStatements(); final PsiStatement lastStatement = statements[statements.length - 1]; for (PsiStatement statement : statements) { if (nextStatement == null) { if (statement == lastStatement && statement instanceof PsiReturnStatement) { final PsiReturnStatement returnStatement = (PsiReturnStatement)statement; if (returnStatement.getReturnValue() == null) { registerStatementError(ifStatement); } } return; } else if (!EquivalenceChecker.getCanonicalPsiEquivalence().statementsAreEquivalent( statement, nextStatement)) { return; } nextStatement = getNextStatement(nextStatement); } } else if (!EquivalenceChecker.getCanonicalPsiEquivalence().statementsAreEquivalent( thenBranch, nextStatement)) { return; } registerStatementError(ifStatement); }
@Override public void visitReturnStatement(PsiReturnStatement statement) { mVisitor.report("PsiReturnStatement", statement.getText(), statement); super.visitStatement(statement); }
public WrapReturnValue(@NotNull PsiReturnStatement statement, @NotNull String type) { super(statement); myStatement = statement; myType = type; }
public ReturnWrappedValue(PsiReturnStatement statement) { super(statement); myStatement = statement; }
public CheckReturnValueInstruction(final PsiReturnStatement aReturn) { myReturn = aReturn; }
public PsiReturnStatement getReturn() { return myReturn; }
public WrapReturnValue(PsiReturnStatement statement, String type) { super(statement); this.type = type; this.statement = statement; }
public ReturnWrappedValue(PsiReturnStatement statement) { super(statement); this.statement = statement; }
@Override public boolean isInsideSimpleGetter(@NotNull PsiElement element) { PsiMethod method = PsiTreeUtil.getParentOfType(element, PsiMethod.class); if(method == null) { return false; } final PsiCodeBlock body = method.getBody(); if(body == null) { return false; } final PsiStatement[] statements = body.getStatements(); if(statements.length != 1) { return false; } final PsiStatement statement = statements[0]; if(!(statement instanceof PsiReturnStatement)) { return false; } final PsiExpression value = ((PsiReturnStatement) statement).getReturnValue(); if(!(value instanceof PsiReferenceExpression)) { return false; } final PsiReferenceExpression reference = (PsiReferenceExpression) value; final PsiExpression qualifier = reference.getQualifierExpression(); //noinspection HardCodedStringLiteral if(qualifier != null && !"this".equals(qualifier.getText())) { return false; } final PsiElement referent = reference.resolve(); if(referent == null) { return false; } if(!(referent instanceof PsiField)) { return false; } return Comparing.equal(((PsiField) referent).getContainingClass(), method.getContainingClass()); }
/** * Handles situations like the one below: * <pre> * void foo(int i) { * if (i < 0) { * return;[caret] * } * } * </pre> * * <b>Output:</b> * <pre> * void foo(int i) { * if (i < 0) { * return; * } * [caret] * } * </pre> * * @param element * @return */ private static boolean isControlFlowBreak(@Nullable PsiElement element) { return element instanceof PsiReturnStatement || element instanceof PsiThrowStatement; }
/** * Handles situations like the one below: * <pre> * void foo(int i) { * if (i < 0) { * return;[caret] * } * } * </pre> * <p> * <b>Output:</b> * <pre> * void foo(int i) { * if (i < 0) { * return; * } * [caret] * } * </pre> * * @param element * @return */ private static boolean isControlFlowBreak(@Nullable PsiElement element) { return element instanceof PsiReturnStatement || element instanceof PsiThrowStatement; }
void visit(final List<PsiReturnStatement> returnStatements) throws IncorrectOperationException;