Z022: @This() alias in anonymous struct should be Self
In anonymous or local structs, the @This() alias should be named Self.
Examples
Bad
fn MakeCounter() type {
return struct {
// expect: Z022
const Counter = @This();
count: u32 = 0,
};
}Good
Use Self for anonymous structs:
fn MakeCounter() type {
return struct {
const Self = @This();
count: u32 = 0,
};
}
Named top-level structs should use their name instead (see Z019):
const Counter = struct {
count: u32 = 0,
pub fn increment(self: *Counter) void {
self.count += 1;
}
};Rationale
Self is the conventional name for @This() in anonymous structs throughout the Zig ecosystem. Using a different name is confusing and non-idiomatic. Since the struct has no external name, Self makes it clear you're referring to "this struct."