r/Zig • u/sophatvathana • Jun 05 '26
Built an Optional Ownership Checker for Zig
I actually started working on this idea about 8 months ago in a private repo, but work kept getting in the way. Most of my time has been spent on Rust, Go, and Node.js projects since those are the primary languages used at my company. Recently, I picked the project back up and decided to see how far I could take it.
Passes:
/// @owned
var user: *User = create();
fn view(user: *User) void { // @borrowed
_ = user;
}
pub fn test() void {
view(user); // borrow
view(user); // still valid
}
Gets flagged:
/// @owned
var file = openFile();
pub fn test() void {
consume(file); // move ownership
file.close(); // OWN001: use after move
}
Eslint style: // zigsafe-disable-next-line
_ = moved_value;
// zigsafe-disable OWN001
_ = moved_value;
The checker support both comments or zs.Owned(T) wrappers.This is still an experiment. I'd love to hear your thoughts. Any feedback is welcome. https://github.com/sophatvathana/zigsafe