void (@NotNull ActionContext context, @NotNull PsiVariable variable, @NotNull ModPsiUpdater updater) { boolean retry = true; while(retry) { retry = false; List<PsiReferenceExpression> refs = collectReferences(variable); for (PsiReferenceExpression ref : refs) { if (!ref.isValid()) { retry = true; break; } PsiElement parent = PsiUtil.skipParenthesizedExprUp(ref.getParent()); if (parent instanceof PsiUnaryExpression unary && ExpressionUtils.isVoidContext(unary)) { RemoveUnusedVariableUtil.deleteWholeStatement(parent); } else if (parent instanceof PsiAssignmentExpression assignment) { PsiExpression rExpression = assignment.getRExpression(); if (ExpressionUtils.isVoidContext(assignment)) { if (rExpression != null && myKeepSideEffects) { RemoveInitializerFix.SideEffectAwareRemove.remove(rExpression); } else { RemoveUnusedVariableUtil.deleteWholeStatement(assignment); } } else if (rExpression != null) { PsiElement result = new CommentTracker().replaceAndRestoreComments(assignment, rExpression); if (result.getParent() instanceof PsiParenthesizedExpression parens && !ParenthesesUtils.areParenthesesNeeded(parens, true)) { parens.replace(result); } } else { RemoveUnusedVariableUtil.deleteWholeStatement(assignment); } } } } if (variable instanceof PsiField) { variable.normalizeDeclaration(); } PsiExpression initializer = variable.getInitializer(); if (initializer != null && myKeepSideEffects) { RemoveInitializerFix.SideEffectAwareRemove.remove(initializer); } CommentTracker tracker = new CommentTracker(); tracker.markUnchanged(variable.getInitializer()); // assume that initializer is used (e.g. inlined) if (variable instanceof PsiJavaDocumentedElement docElement) { tracker.markUnchanged(docElement.getDocComment()); } tracker.deleteAndRestoreComments(variable); }