ziglint Changelog View on GitHub

Z027: Access declaration through type instead of instance

Detects accessing struct constants or static functions through an instance when they should be accessed through the type.

Examples

Bad

const Foo = struct {
    field: u32,
    const bar = 42;
};
const instance: Foo = .{ .field = 0 };
// expect: Z027
pub fn main() void {
    _ = instance.bar;
}

Good

Access through the type:

const Foo = struct {
    field: u32,
    const bar = 42;
};
pub fn main() void {
    _ = Foo.bar;
}

Instance methods (with self parameter) are fine:

pub const Foo = struct {
    field: u32,
    pub fn getField(self: Foo) u32 {
        return self.field;
    }
};
const instance: Foo = .{ .field = 0 };
pub fn main() void {
    _ = instance.getField();
}

Rationale

Accessing type-level declarations through instances is misleading—it suggests the value depends on the instance when it doesn't. Using the type name makes it clear the declaration is static and independent of any instance.

Note: This rule requires semantic analysis to distinguish instance methods from static functions.