boolean (PsiLocalVariable var) { if (var == null) return false; PsiElement block = PsiUtil.getVariableCodeBlock(var, null); return block != null && getVariableReferences(var, block).stream() .map(ref -> PsiTreeUtil.getParentOfType(ref, PsiClass.class, PsiLambdaExpression.class)) .allMatch(context -> context == null || PsiTreeUtil.isAncestor(context, block, false)); }