field_reassign_with_default
Finds x: T; x.a = 1; x.b = 2; where a struct literal x := T.{ a = 1, b = 2 } sets the fields at the declaration.
Category pedantic, allow by default.
Off by default; the example is shown with the rule enabled.
#import "Basic";
Config :: struct {
width: int = 80;
height: int = 24;
title: string;
}
make :: (title: string) -> Config {
config: Config;
config.width = 100;
config.title = title;
return config;
}
main :: () {
print("%\n", make("demo"));
}warning[field_reassign_with_default]: `config` is made with its defaults and then its fields set
--> example.jai:10:5
|
10 | config: Config;
| ^^^^^^
|
help: set the fields in a struct literal where the variable is declared
|
10 - config: Config;
11 - config.width = 100;
12 - config.title = title;
10 + config := Config.{ width = 100, title = title };
|#import "Basic";
Config :: struct {
width: int = 80;
height: int = 24;
title: string;
}
make :: (title: string) -> Config {
return .{ width = 100, title = title };
}
main :: () {
print("%\n", make("demo"));
}The fields not named in the literal keep their defaults, as in the declaration, and the values are set where the variable is made instead of in statements a later edit can separate from it.