cockroachdb / cockroachdb/cockroach
add linter for large struct initialization without field keys
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
Initializing large structs without using explicit field keys leads to code that is unreadable (outside of a good IDE) and very hard to modify. Adding a new field becomes a nightmare, even if it is supposed to be set to zero in most cases.
Typically such cases don't pass code review but some have made it in, for example:
https://github.com/cockroachdb/cockroach/blob/98b5d11cf8a07fa677091827f79d7fda0ebf41d1/pkg/cmd/roachtest/github_test.go#L144
Note that we initialize most fields to zero so the explicit version is actually fairly succint:
```diff
- {true, false, false, false, false, nil, "", createFailure(errors.New("other")), true, false, false,
- prefixAll(map[string]string{
+ {
+ nonReleaseBlocker: true,
+ failure: createFailure(errors.New("other")),
+ expectedPost: true,
+ expectedParams: prefixAll(map[string]string{
```
We should disallow "inline" struct initialization when there are more than 1 or 2 fields.
Jira issue: CRDB-33942
Contributor guide
Assessment
This issue has not been assessed yet.