void (PsiSwitchLabelStatementBase label) { if (shouldRemoveBranch(label)) { PsiCodeBlock scope = ObjectUtils.tryCast(label.getParent(), PsiCodeBlock.class); if (scope == null) return; PsiSwitchLabelStatementBase nextLabel = PsiTreeUtil.getNextSiblingOfType(label, PsiSwitchLabelStatementBase.class); PsiElement stopAt = nextLabel == null ? scope.getRBrace() : nextLabel; while(true) { PsiStatement next = PsiTreeUtil.getNextSiblingOfType(nextLabel, PsiStatement.class); if (!(next instanceof PsiSwitchLabelStatement)) break; nextLabel = (PsiSwitchLabelStatement)next; } int end = nextLabel == null ? -1 : nextLabel.getTextOffset(); List<PsiElement> toDelete = new ArrayList<>(); List<PsiDeclarationStatement> declarations = new ArrayList<>(); for (PsiElement e = label.getNextSibling(); e != stopAt; e = e.getNextSibling()) { if (e instanceof PsiDeclarationStatement declaration && nextLabel != null) { PsiElement[] elements = declaration.getDeclaredElements(); boolean declarationIsReused = ContainerUtil.or(elements, element -> ReferencesSearch.search(element, new LocalSearchScope(scope)).anyMatch(ref -> ref.getElement().getTextOffset() > end)); if (declarationIsReused) { StreamEx.of(elements).select(PsiVariable.class).map(PsiVariable::getInitializer).nonNull().into(toDelete); declarations.add(declaration); continue; } } toDelete.add(e); } CommentTracker ct = new CommentTracker(); toDelete.stream().filter(PsiElement::isValid).forEach(ct::delete); for (PsiDeclarationStatement declaration : declarations) { scope.addAfter(declaration, nextLabel); declaration.delete(); } ct.insertCommentsBefore(label); } new CommentTracker().deleteAndRestoreComments(label); }