     appendToken(.break(.same, size: 0))
   }
 
+  /// Cleans up state related to inserting contextual breaks throughout expressions during
+  /// `visitPost` for an expression that is the root of an expression tree.
+  private func clearContextualBreakState<T: ExprSyntaxProtocol>(_ expr: T) {
+    let exprID = expr.id
+    if rootExprs.remove(exprID) != nil {
+      preVisitedExprs.removeAll()
+    }
+  }
+
   /// Visits the given expression node and all of the nested expression nodes, inserting tokens
   /// necessary for contextual breaking throughout the expression. Records the nodes that were
   /// visited so that they can be skipped later.
   private func preVisitInsertingContextualBreaks<T: ExprSyntaxProtocol & Equatable>(_ expr: T) {
-    let exprSyntax = ExprSyntax(expr)
-    if !preVisitedExprs.contains(exprSyntax) {
-      let (visited, _, _) = insertContextualBreaks(exprSyntax, isTopLevel: true)
-      preVisitedExprs.formUnion(visited)
+    let exprID = expr.id
+    if !preVisitedExprs.contains(exprID) {
+      rootExprs.insert(exprID)
+      insertContextualBreaks(ExprSyntax(expr), isTopLevel: true)
     }
   }
 
   /// Recursively visits nested expressions from the given expression inserting contextual breaking
   /// tokens. When visiting an expression node, `preVisitInsertingContextualBreaks(_:)` should be
   /// called instead of this helper.
+  @discardableResult
   private func insertContextualBreaks(_ expr: ExprSyntax, isTopLevel: Bool) -> (
-    [ExprSyntax], hasCompoundExpression: Bool, hasMemberAccess: Bool
+    hasCompoundExpression: Bool, hasMemberAccess: Bool
   ) {
+    preVisitedExprs.insert(expr.id)
     if let memberAccessExpr = expr.as(MemberAccessExprSyntax.self) {
       // When the member access is part of a calling expression, the break before the dot is
       // inserted when visiting the parent node instead so that the break is inserted before any


  /// Appends the before-tokens of the given syntax token to the token stream.
  private func appendBeforeTokens(_ token: TokenSyntax) {
    if let before = beforeMap[token] {
      before.forEach(appendToken)
    }
  }

      forcesBreakBeforeRightDelimiter: breakBeforeRightBracket)

    return .visitChildren
  }

  override func visitPost(_ node: SubscriptExprSyntax) {
    clearContextualBreakState(node)
  }

  override func visit(_ node: ExpressionSegmentSyntax) -> SyntaxVisitorContinueKind {
