feature. See also
. The project being documented here (as the example) is the Zig library itself.
AstGen.checkUsed
fn checkUsed(gz: *GenZir, outer_scope: *Scope, inner_scope: *Scope) InnerError!void
File
Code
fn checkUsed(gz: *GenZir, outer_scope: *Scope, inner_scope: *Scope) InnerError!void {
const astgen = gz.astgen;
var scope = inner_scope;
while (scope != outer_scope) {
switch (scope.unwrap()) {
.gen_zir => |gen_zir| scope = gen_zir.parent,
.local_val => |s| {
if (s.used == .none and s.discarded == .none) {
try astgen.appendErrorTok(s.token_src, "unused {s}", .{@tagName(s.id_cat)});
} else if (s.used != .none and s.discarded != .none) {
try astgen.appendErrorTokNotes(s.discarded.unwrap().?, "pointless discard of {s}", .{@tagName(s.id_cat)}, &[_]u32{
try gz.astgen.errNoteTok(s.used.unwrap().?, "used here", .{}),
});
}
scope = s.parent;
},
.local_ptr => |s| {
if (s.used == .none and s.discarded == .none) {
try astgen.appendErrorTok(s.token_src, "unused {s}", .{@tagName(s.id_cat)});
} else {
if (s.used != .none and s.discarded != .none) {
try astgen.appendErrorTokNotes(s.discarded.unwrap().?, "pointless discard of {s}", .{@tagName(s.id_cat)}, &[_]u32{
try astgen.errNoteTok(s.used.unwrap().?, "used here", .{}),
});
}
if (s.id_cat == .@"local variable" and !s.used_as_lvalue) {
try astgen.appendErrorTokNotes(s.token_src, "local variable is never mutated", .{}, &.{
try astgen.errNoteTok(s.token_src, "consider using 'const'", .{}),
});
}
}
scope = s.parent;
},
.defer_normal, .defer_error => |defer_scope| scope = defer_scope.parent,
.namespace => unreachable,
.top => unreachable,
}
}
}