ReturnContext (PsiReturnStatement returnStatement) { final PsiElement returnParent = returnStatement.getParent(); if (returnParent instanceof PsiCodeBlock returnScope) { final PsiStatement[] statements = returnScope.getStatements(); if (statements.length != 0 && statements[statements.length - 1] == returnStatement) { final PsiType returnType = PsiTypesUtil.getMethodReturnType(returnStatement); if (returnType != null) { PsiStatement refactoredStatement = getPrevNonEmptyStatement(returnStatement, null); if (refactoredStatement != null) { final PsiExpression returnValue = PsiUtil.skipParenthesizedExprDown(returnStatement.getReturnValue()); if (returnValue instanceof PsiReferenceExpression ref && ref.resolve() instanceof PsiVariable returnedVariable) { final PsiCodeBlock variableScope = getVariableScopeBlock(returnedVariable); if (variableScope != null) { PsiElement variableMethod = PsiTreeUtil.getParentOfType(variableScope, PsiMethod.class, PsiLambdaExpression.class); PsiElement returnMethod = PsiTreeUtil.getParentOfType(returnScope, PsiMethod.class, PsiLambdaExpression.class); if (variableMethod == returnMethod) { return new ReturnContext(returnStatement, returnScope, returnType, refactoredStatement, returnedVariable, variableScope); } } } } } } } return null; }