A demo of a recent careless accident.
const std = @import("std");
fn foo(n: i32) void {
if (n == 0) return
if (n < 0) std.debug.print("< 0 \n", .{})
else std.debug.print("> 0 \n", .{});
}
pub fn main() void {
foo(0);
}
A demo of a recent careless accident.
const std = @import("std");
fn foo(n: i32) void {
if (n == 0) return
if (n < 0) std.debug.print("< 0 \n", .{})
else std.debug.print("> 0 \n", .{});
}
pub fn main() void {
foo(0);
}
Outputs > 0
due to missing semicolon on return. Itβs kinda sneaky for sure though.
zig fmt
makes the problem clearer:
const std = @import("std");
fn foo(n: i32) void {
if (n == 0) return if (n < 0) std.debug.print("< 0 \n", .{}) else std.debug.print("> 0 \n", .{});
}
pub fn main() void {
foo(0);
}
One of the reasons why I have fmt on save enabled.
Interesting example though, as zig fmt
wonβt help much if the second if
has a comment.
Even then it helps:
if (n == 0) return if (n < 0) std.debug.print("< 0 \n", .{}) // something
else std.debug.print("> 0 \n", .{});
Relevant accepted proposal:
Try with this (zig fmt does nothing for me here):
fn foo(n: i32) void {
if (n == 0) return
// Some comment
if (n < 0) std.debug.print("< 0 \n", .{}) else std.debug.print("> 0 \n", .{});
}
After saving, an error is immediately displayed in the editor:
Expected expression, found β;β
I think that should be enough to recognize that something is wrong.
No error in my editor using zls and also the program compiles just fine: The Zig Pastebin
Was my bad, made a copy/paste error. Indeed, then itβs not easy to see the error immediately.
no sympathy for those who forsake zig fmt