void (PsiElement result) { if (!(result instanceof PsiExpressionStatement)) return; PsiAssignmentExpression assignment = tryCast(((PsiExpressionStatement)result).getExpression(), PsiAssignmentExpression.class); if (assignment == null) return; if (!assignment.getOperationTokenType().equals(JavaTokenType.EQ)) return; PsiReferenceExpression ref = tryCast(assignment.getLExpression(), PsiReferenceExpression.class); if (ref == null) return; PsiDeclarationStatement declaration = tryCast(PsiTreeUtil.skipWhitespacesAndCommentsBackward(result), PsiDeclarationStatement.class); if (declaration == null) return; PsiElement[] elements = declaration.getDeclaredElements(); if (elements.length != 1) return; PsiLocalVariable var = tryCast(elements[0], PsiLocalVariable.class); if (var == null || !ref.isReferenceTo(var)) return; final PsiExpression rhs = assignment.getRExpression(); assert rhs != null; boolean readBeforeWritten = SyntaxTraverser.psiTraverser(rhs) .filter(PsiReferenceExpression.class) .filter(r -> r.isReferenceTo(var) && PsiUtil.isAccessedForReading(r)) .isNotEmpty(); if (readBeforeWritten) return; CommentTracker ct = new CommentTracker(); var.setInitializer(ct.markUnchanged(rhs)); ct.deleteAndRestoreComments(result); }