Z002: Named discard
Detects initialized variables with a single-underscore prefix such as _result. Zig does not treat these names as discards: they are ordinary variables that merely look unused.
Examples
Bad
// expect: Z002
const _unused = getValue();Good
If the value is intentionally discarded, use a discard assignment:
fn getValue() u32 {
return 42;
}
fn discardValue() void {
_ = getValue();
}
If the value is used, give it a regular name:
fn getValue() u32 {
return 42;
}
fn use(value: u32) void {
_ = value;
}
fn processValue() void {
const result = getValue();
use(result);
}
Rationale
Named discards like _unused are ambiguous: the prefix suggests the value is intentionally ignored, but Zig still creates an ordinary binding. A discard assignment makes intentional disposal explicit, while a regular name communicates that the value is used.