PsiLocalVariable (PsiMethod methodCopy, PsiReference callSite, PsiType returnType) { if (returnType == null || PsiTypes.voidType().equals(returnType) || callSite.getElement().getParent() instanceof PsiMethodCallExpression && ExpressionUtils.isVoidContext((PsiExpression)callSite.getElement().getParent())) { InlineUtil.extractReturnValues(methodCopy, false); return null; } PsiCodeBlock block = Objects.requireNonNull(methodCopy.getBody()); Project project = methodCopy.getProject(); PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); String resultName = new VariableNameGenerator(block, VariableKind.LOCAL_VARIABLE).byName("result", "res").generate(true); PsiDeclarationStatement declaration = factory.createVariableDeclarationStatement(resultName, returnType, null); declaration = (PsiDeclarationStatement)block.addAfter(declaration, null); PsiLocalVariable resultVar = (PsiLocalVariable)declaration.getDeclaredElements()[0]; CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project); for (PsiReturnStatement returnStatement : PsiUtil.findReturnStatements(block)) { final PsiExpression returnValue = returnStatement.getReturnValue(); if (returnValue == null) continue; PsiStatement statement = factory.createStatementFromText(resultName + "=0;", null); statement = (PsiStatement)codeStyleManager.reformat(statement); PsiAssignmentExpression assignment = (PsiAssignmentExpression)((PsiExpressionStatement)statement).getExpression(); Objects.requireNonNull(assignment.getRExpression()).replace(returnValue); returnStatement.replace(statement); } return resultVar; }