szn

Bug 560 — Dead-code and duplication cluster 5

560. Unreachable branches, unused definitions, and drifted duplicate helpers

Severity: LOW (code quality)
Status: ✅ RESOLVED
Files: src/server/server.zig:1255-1301, src/format.zig:377-411, src/cmd/cmd.zig:21, src/key.zig:231-279, src/client/client.zig:8-21, src/log.zig:210-262

Dead Code

Site Finding
src/server/server.zig:1255,1273-1301 Inside writeKeyToPty’s .char => \|ch\| arm, ch.code == 9 and switch (ch.code) for Kitty Private Use Area codepoints 57344..57353 are unreachable because key.parseSingleByte (src/key.zig:350) and key.parseCsi (src/key.zig:196-206) already decode those values into .special and .arrow.
src/format.zig:377-380,406-411 ExpandResult and expandContent are never referenced anywhere in production or tests.
src/cmd/cmd.zig:21, src/server/dispatch.zig:86-91 CmdResult.wait is never constructed by any command handler.
src/input.zig:975, src/choose.zig:115, src/server/render.zig:452 InputParser.dispatchOsc, ChooseMode.handleKey, and Display.drawLayoutBorders return error unions (Error!void, Error!Action, tty.Error!void) despite having no error return paths.

Duplicate Code & Drifted Logic

Site Finding
src/key.zig:231-245 vs :268-279 key.parse inlines an ESC O (SS3) switch that duplicates key.parseSs3 — and the inline copy in key.parse omits 'H' (Home) and 'F' (End).
src/cmd/cmd.zig:337-374 vs src/server/server.zig:1409-1434 cmdSwapPane and Server.swapPaneRelative duplicate leaf-node swapping, and cmdSwapPane drifted by omitting resizePaneToNode, invalidateBorderFormat, and server.dirty = true (see Bug #558).
src/cmd/cmd.zig:1280-1331 vs :1370-1409 cmdBindKey and cmdUnbindKey duplicate the -r and -T <table> flag parsing loop.
src/client/client.zig:8-21 vs src/io.zig:36-49 client.writeAll duplicates io.writeAll without retrying on EINTR or yielding on EAGAIN.
src/log.zig:210-262 log.enable and log.enableClientLog are nearly identical functions differing only in the default fallback filename (szn-server.log vs szn-client.log).
src/main.zig:27-34,100-130 vs src/server/server.zig:533-545,3766-3773 currentMillis() and setNonBlocking() are duplicated across main.zig, server.zig, and pty.zig.

Proposed Fix

  1. Delete the unreachable codepoints in writeKeyToPty, unused ExpandResult/expandContent in format.zig, and unused CmdResult.wait, and drop the unused error unions on dispatchOsc, ChooseMode.handleKey, and drawLayoutBorders.
  2. Call parseSs3(input[2]) from key.parse, share a single pane-swap helper between cmdSwapPane and Server.swapPaneRelative, extract a shared bind/unbind flag parser in cmd.zig, route client.writeAll through io.writeAll, and consolidate log.enable/enableClientLog, currentMillis, and setNonBlocking.

Resolution

  1. Dead code removed:
    • Removed unreachable ch.code == 9 and 57344..57353 branches from Server.writeKeyToPty’s .char arm (and updated the Kitty functional-key unit test to exercise key.parse + Server.writeKeyToPty).
    • Removed unused ExpandResult/expandContent in src/format.zig and CmdResult.wait in src/cmd/cmd.zig / src/server/dispatch.zig.
    • Removed unused error unions from InputParser.dispatchOsc (src/input.zig), ChooseMode.handleKey (src/choose.zig), and Display.drawLayoutBorders (src/server/render.zig).
  2. Duplication consolidated:
    • Routed key.parse’s ESC O (SS3) branch through key.parseSs3(seq[2]) in src/key.zig.
    • Extracted Server.swapPanesAt in src/server/server.zig and shared it between Server.swapPaneRelative and cmdSwapPane (src/cmd/cmd.zig).
    • Extracted parseBindKeyFlags in src/cmd/cmd.zig and shared it between cmdBindKey and cmdUnbindKey.
    • Added io.writeAll, io.currentMillis, io.setNonBlocking, and io.setBlocking in src/io.zig and routed src/client/client.zig, src/main.zig, src/server/pty.zig, and src/server/server.zig through them.
    • Consolidated log.enable and log.enableClientLog in src/log.zig via enableWithResolver.