PsiElement (PsiCodeBlock block) { if (block == null || !block.isEmpty()) return block; PsiBlockStatement blockStatement = tryCast(block.getParent(), PsiBlockStatement.class); if (blockStatement == null) return block; PsiIfStatement parent = tryCast(blockStatement.getParent(), PsiIfStatement.class); if (parent == null) return block; PsiExpression condition = parent.getCondition(); if (condition == null) return block; if (blockStatement == parent.getElseBranch()) { new CommentTracker().deleteAndRestoreComments(blockStatement); return parent; } if (blockStatement == parent.getThenBranch()) { if (parent.getElseBranch() != null) { new CommentTracker().replaceAndRestoreComments(blockStatement, parent.getElseBranch()); parent.getElseBranch().delete(); CommentTracker ct = new CommentTracker(); String negatedCondition = BoolUtils.getNegatedExpressionText(condition, ct); ct.replaceAndRestoreComments(condition, negatedCondition); return parent; } if (!SideEffectChecker.mayHaveSideEffects(condition)) { PsiCodeBlock parentBlock = tryCast(parent.getParent(), PsiCodeBlock.class); new CommentTracker().deleteAndRestoreComments(parent); return cleanUpEmptyBlocks(parentBlock); } } return block; }