Skip to content

zig fmt: bad indentation of catch block inside while without block #5722

@ifreund

Description

@ifreund

I've noticed some strange behavior with zig fmt on a catch block inside a while loop without a block:

Current behavior:

pub fn sendViewTags(self: Self) void {
    ...
    var it = ViewStack(View).iterator(self.output.views.first, std.math.maxInt(u32));
    while (it.next()) |node|
        view_tags.append(node.view.current_tags) catch {
        c.wl_resource_post_no_memory(self.wl_resource);
        log.crit(.river_status, "out of memory", .{});
        return;
    };
    ...
}

Desired behavior:

pub fn sendViewTags(self: Self) void {
    ...
    var it = ViewStack(View).iterator(self.output.views.first, std.math.maxInt(u32));
    while (it.next()) |node|
        view_tags.append(node.view.current_tags) catch {
            c.wl_resource_post_no_memory(self.wl_resource);
            log.crit(.river_status, "out of memory", .{});
            return;
        };
    ...
}

Works fine if you use a block with the while loop:

pub fn sendViewTags(self: Self) void {
    ...
    var it = ViewStack(View).iterator(self.output.views.first, std.math.maxInt(u32));
    while (it.next()) |node| {
        view_tags.append(node.view.current_tags) catch {
            c.wl_resource_post_no_memory(self.wl_resource);
            log.crit(.river_status, "out of memory", .{});
            return;
        };
    }
    ...
}

Alternative also disallowed by zig fmt currently that I think should be allowed:

pub fn sendViewTags(self: Self) void {
    ...
    var it = ViewStack(View).iterator(self.output.views.first, std.math.maxInt(u32));
    while (it.next()) |node| view_tags.append(node.view.current_tags) catch {
        c.wl_resource_post_no_memory(self.wl_resource);
        log.crit(.river_status, "out of memory", .{});
        return;
    };
    ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorcontributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.frontendTokenization, parsing, AstGen, Sema, and Liveness.standard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions