Z025: Redundant catch returning error
Detects catch |err| return err patterns which can be replaced with try.
Examples
Bad
// expect: Z025
fn process() !void {
const result = doWork() catch |err| return err;
_ = result;
}Good
Use try instead:
fn process() !void {
const result = try doWork();
_ = result;
}
A different catch body is fine:
fn process() !void {
const result = doWork() catch |err| {
log.err("failed: {}", .{err});
return err;
};
_ = result;
}
Returning a different error is also fine:
fn process() !void {
const result = doWork() catch return error.ProcessFailed;
_ = result;
}Rationale
try expr is syntactic sugar for expr catch |err| return err. Using the explicit catch form when you're just returning the error is verbose and obscures the intent. try is clearer and more idiomatic.