Z004: Prefer anonymous struct init with type annotation
Prefer const x: T = .{} over const x = T{}. The type annotation style is more consistent with Zig idioms and works better with type inference.
Keep the original const or var, declaration modifiers, and initializer fields when making this change.
Examples
Bad
// expect: Z004
const point = Point{ .x = 0, .y = 0 };// expect: Z004
const config = Config{};Good
const point: Point = .{ .x = 0, .y = 0 };const config: Config = .{};var point: Point = .{ .x = 0, .y = 0 };Rationale
The type annotation style const x: T = .{} is preferred because:
- It's consistent with how Zig handles type inference elsewhere
- It makes the type visible at the declaration site
- It works better when the type comes from a function return or generic
- It's the style used in the Zig standard library