boolean (@NotNull PsiLocalVariable variable, @NotNull PsiStatement sourceStatement) { NavigatablePsiElement loopBound = PsiTreeUtil.getParentOfType(sourceStatement, PsiMember.class, PsiLambdaExpression.class); Predicate<PsiReference> referenceBoundPredicate; if (sourceStatement instanceof PsiLoopStatement) { referenceBoundPredicate = (reference) -> PsiTreeUtil.getParentOfType(reference.getElement(), PsiMember.class, PsiLambdaExpression.class) == loopBound; } else { referenceBoundPredicate = (reference) -> { PsiLambdaExpression lambda = PsiTreeUtil.getParentOfType(reference.getElement(), PsiLambdaExpression.class); return PsiTreeUtil.getParentOfType(lambda, PsiMember.class, PsiLambdaExpression.class) == loopBound; }; } return ReferencesSearch.search(variable).allMatch(referenceBoundPredicate) && FinalUtils.canBeFinal(variable); }