From 4917ae452a15ff4f32d97d215a99fdcfaab29d61 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 6 Nov 2026 13:13:17 -0300 Subject: idioms #9, every catch unreachable this slice added is a fact of the code: each formats integers, literals, an error's name or a slice of a fixed array (the daemon's [128]u8 names, the cache's path_buf, main's cwd_buf, those two buffers now sized from their arrays), so all 39 stay, each with its reason on its own line Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/daemon.zig | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) (limited to 'src/daemon.zig') diff --git a/src/daemon.zig b/src/daemon.zig index 009b81e3..dc86963d 100644 --- a/src/daemon.zig +++ b/src/daemon.zig @@ -361,9 +361,8 @@ pub fn setState(detached: bool) void { pub fn clients(pids: []const libc.pid_t) void { var buf: [64]u8 = undefined; var w: std.Io.Writer = .fixed(&buf); - // "clients" and max_clients pids fit 64. - w.writeAll("clients") catch unreachable; - for (pids[0..@min(pids.len, max_clients)]) |pid| w.print(" {d}", .{pid}) catch unreachable; + w.writeAll("clients") catch unreachable; // a literal fits 64 + for (pids[0..@min(pids.len, max_clients)]) |pid| w.print(" {d}", .{pid}) catch unreachable; // with max_clients pids, fits 64 tell(w.buffered(), &.{}); } @@ -764,14 +763,13 @@ const Daemon = struct { fn kill(d: *Daemon, i: u8, named: ?[]const u8) void { var line: [msg_max]u8 = undefined; var w: std.Io.Writer = .fixed(&line); - // "ok" and max_conns pids fit msg_max. - w.writeAll("ok") catch unreachable; + w.writeAll("ok") catch unreachable; // a literal fits msg_max var n: usize = 0; for (&d.conns) |*s| if (s.fd >= 0 and s.kind != null) { if (named) |want| if (!std.mem.eql(u8, s.named(), want)) continue; s.killed = true; _ = libc.kill(s.pid, .TERM); - w.print(" {d}", .{s.pid}) catch unreachable; + w.print(" {d}", .{s.pid}) catch unreachable; // max_conns pids after "ok" fit msg_max n += 1; }; if (named == null) { @@ -789,7 +787,7 @@ const Daemon = struct { fn list(d: *Daemon, i: u8) void { var line: [msg_max]u8 = undefined; var w: std.Io.Writer = .fixed(&line); - w.print("daemon {d}\n", .{libc.getpid()}) catch unreachable; // the first line of msg_max + w.print("daemon {d}\n", .{libc.getpid()}) catch unreachable; // a pid fits msg_max for (&d.conns, 0..) |*s, k| if (s.fd >= 0 and s.kind != null) { w.print("session {d} {t} {s} {s}\n", .{ s.pid, s.kind.?, if (s.detached) "detached" else "attached", s.named() }) catch break; for (s.clients[0..s.nclients]) |pid| w.print("client {d}\n", .{pid}) catch break; @@ -862,7 +860,7 @@ const Daemon = struct { .adopt => { var line: [msg_max]u8 = undefined; var w: std.Io.Writer = .fixed(&line); - w.writeAll("adopted") catch unreachable; // the first word of msg_max + w.writeAll("adopted") catch unreachable; // a literal fits msg_max var fds: [max_fds]c_int = undefined; var given: [max_fds]*Held = undefined; var n: usize = 0; @@ -888,8 +886,7 @@ const Daemon = struct { } t.detached = false; var line: [32]u8 = undefined; - // "ok " and a pid_t's at most 11 digits fit 32. - return d.reply(i, std.fmt.bufPrint(&line, "ok {d}", .{t.pid}) catch unreachable, &.{}); + return d.reply(i, std.fmt.bufPrint(&line, "ok {d}", .{t.pid}) catch unreachable, &.{}); // a pid fits 32 }, } } @@ -936,7 +933,7 @@ const Daemon = struct { found = @intCast(i); }; if (count == 1) { - _ = std.fmt.bufPrint(why, "other {s}", .{d.conns[found.?].named()}) catch unreachable; // a name of 128 fits 512 + _ = std.fmt.bufPrint(why, "other {s}", .{d.conns[found.?].named()}) catch unreachable; // a [128]u8 field's name fits 512 return null; } count = 0; @@ -948,19 +945,19 @@ const Daemon = struct { only = s; }; if (found == null and sessions == 1) { - _ = std.fmt.bufPrint(why, "other {s}", .{only.?.named()}) catch unreachable; // a name of 128 fits 512 + _ = std.fmt.bufPrint(why, "other {s}", .{only.?.named()}) catch unreachable; // a [128]u8 field's name fits 512 return null; } } if (count == 0 and others == 0) { - w.writeAll("no no detached session is running") catch unreachable; // fits 511 + w.writeAll("no no detached session is running") catch unreachable; // a literal fits 511 return null; } if (count == 0) { - w.print("no the detached session{s} running {s} {s}=`", .{ if (others == 1) " is" else "s are", if (others == 1) "is" else "are each", other }) catch unreachable; // fits 511 + w.print("no the detached session{s} running {s} {s}=`", .{ if (others == 1) " is" else "s are", if (others == 1) "is" else "are each", other }) catch unreachable; // literals fit 511 return null; } - w.print("no {d} detached sessions are running; say which with --attach=:", .{count}) catch unreachable; // fits 511 + w.print("no {d} detached sessions are running; say which with --attach=:", .{count}) catch unreachable; // a usize fits 511 for (&d.conns) |*s| if (s.fd >= 0 and s.detached and s.kind == k) { w.print(" {s}", .{s.named()}) catch {}; }; -- cgit v1.3