boolean (PsiMethodCallExpression expression) { final PsiElement parent = PsiTreeUtil.getParentOfType(expression, PsiLoopStatement.class, PsiMember.class, PsiLambdaExpression.class); if (parent instanceof PsiAnonymousClass || parent instanceof PsiLambdaExpression) { // condition is probably checked in a loop somewhere else return true; } if (!(parent instanceof PsiLoopStatement)) { return false; } final PsiStatement body = ((PsiLoopStatement)parent).getBody(); return PsiTreeUtil.isAncestor(body, expression, true); }