TailCallType (@NotNull final PsiReference psiReference) { PsiElement element = psiReference.getElement(); if (element instanceof PsiMethodReferenceExpression) return TailCallType.Return; PsiExpression methodCall = PsiTreeUtil.getParentOfType(element, PsiMethodCallExpression.class); if (methodCall == null) return TailCallType.None; PsiElement callParent = PsiUtil.skipParenthesizedExprUp(methodCall.getParent()); if (callParent instanceof PsiReturnStatement || callParent instanceof PsiLambdaExpression) { return TailCallType.Return; } if (callParent instanceof PsiExpression && BoolUtils.isNegation((PsiExpression)callParent)) { PsiElement negationParent = PsiUtil.skipParenthesizedExprUp(callParent.getParent()); if (negationParent instanceof PsiReturnStatement || negationParent instanceof PsiLambdaExpression) { return TailCallType.Invert; } } if (callParent instanceof PsiExpressionStatement) { PsiStatement curElement = (PsiStatement)callParent; while (true) { if (PsiTreeUtil.getNextSiblingOfType(curElement, PsiStatement.class) != null) return TailCallType.None; PsiElement parent = curElement.getParent(); if (parent instanceof PsiCodeBlock) { PsiElement blockParent = parent.getParent(); if (blockParent instanceof PsiMethod || blockParent instanceof PsiLambdaExpression) return TailCallType.Simple; if (!(blockParent instanceof PsiBlockStatement)) return TailCallType.None; parent = blockParent.getParent(); if (parent instanceof PsiLoopStatement) return TailCallType.Continue; } if (!(parent instanceof PsiLabeledStatement) && !(parent instanceof PsiIfStatement)) return TailCallType.None; curElement = (PsiStatement)parent; } } return TailCallType.None; }