boolean (@NotNull PsiCodeBlock block, @NotNull PsiCodeBlock parentBlock) { final PsiStatement[] statements = block.getStatements(); if (statements.length == 0) { return false; } final int endOffset = block.getTextRange().getEndOffset(); final List<PsiCodeBlock> affectedBlocks = SyntaxTraverser.psiTraverser(parentBlock) .filter(PsiCodeBlock.class) .filter(cb -> cb.getTextRange().getEndOffset() > endOffset) .addAllTo(new SmartList<>()); final Project project = block.getProject(); final JavaPsiFacade facade = JavaPsiFacade.getInstance(project); final PsiResolveHelper resolveHelper = facade.getResolveHelper(); for (final PsiStatement statement : statements) { if (!(statement instanceof PsiDeclarationStatement)) { continue; } final PsiDeclarationStatement declaration = (PsiDeclarationStatement)statement; final PsiElement[] variables = declaration.getDeclaredElements(); for (PsiElement variable : variables) { if (!(variable instanceof PsiLocalVariable)) { continue; } final PsiLocalVariable localVariable = (PsiLocalVariable)variable; final String variableName = localVariable.getName(); for (PsiCodeBlock codeBlock : affectedBlocks) { final PsiVariable target = resolveHelper.resolveAccessibleReferencedVariable(variableName, codeBlock); if (target instanceof PsiLocalVariable) { return true; } if (target instanceof PsiField) { for (PsiCodeBlock affectedBlock : affectedBlocks) { if (!SyntaxTraverser.psiTraverser(affectedBlock).filter(PsiReferenceExpression.class).filter(ref -> ref.resolve() == target).isEmpty()) { return true; } } } } } } return false; }