Z026: Empty catch block suppresses errors
Detects empty catch {} blocks that silently discard errors.
Examples
Bad
// expect: Z026
fn process() void {
const result = riskyOperation() catch {};
_ = result;
}// expect: Z026
fn load() void {
const data = readFile() catch |_| {};
_ = data;
}Good
Handle the error explicitly:
fn process() void {
const result = riskyOperation() catch |err| {
log.warn("operation failed: {}", .{err});
return;
};
use(result);
}
Or propagate it:
fn process() !void {
const result = try riskyOperation();
use(result);
}
Empty catches in defer/errdefer are allowed for cleanup:
fn process() !void {
const file = try openFile();
defer file.close() catch {}; // OK in defer
// ...
}
Using @panic or unreachable is allowed:
const result = operation() catch @panic("unexpected");
const other = mustSucceed() catch unreachable;Rationale
Empty catch blocks hide errors, making debugging difficult. If an operation can fail, you should either handle the error, propagate it, or explicitly document why ignoring it is safe. Silent error suppression is a common source of hard-to-find bugs.
Most empty catches come from best-effort stdio writes at shutdown or logging paths. The preferred fix is a higher-order API that swallows the error once, like std.debug.print does, so call sites return void and never need a catch at all:
// One deliberate swallow, in one place
fn eprint(io: *Io, comptime fmt: []const u8, args: anytype) void {
var buf: [4096]u8 = undefined;
var w = std.fs.File.stderr().writer(&buf);
w.print(fmt, args) catch return;
w.flush() catch return;
}
// Call sites stay clean
fn shutdown(e: *Engine) void {
eprint(io, "engine stopped, {d} connections closed", .{e.count});
}