Z020: Inline @This()
Detects inline usage of @This(). Use the enclosing type's name in a named struct, or assign @This() to Self in an anonymous struct.
Examples
Bad
const MyStruct = struct {
value: u32,
// expect: Z020
pub fn clone(self: @This()) @This() {
return self;
}
};Good
For named structs, use the name directly (see Z019):
pub const MyStruct = struct {
value: u32,
pub fn clone(self: MyStruct) MyStruct {
return self;
}
};
For anonymous structs, assign @This() to Self first (see Z022).
Rationale
Inline @This() is verbose and harder to read than a named constant. Assigning it to Self (or the struct name) once at the top of the struct makes the code cleaner and follows Zig conventions.