Anonymous struct types are nested three levels deep

tx0/go:nested-anonymous-structstrictwarningmaintainability

What it finds

Spec struct { Limits struct { CPU string } } inside another anonymous struct: types written in place have no name, so they cannot be used in function signatures, their values cannot be built without repeating the whole type, and deep nesting makes the shape hard to read. A named type for the inner structure avoids that.

Reported is an anonymous struct type written inside another anonymous struct type that is itself nested in a struct, that is, the third level of anonymous structs. A single level, common for configuration and test tables, is left alone.

From the rule's source, nested_anonymous_struct.rs.

Example from the rule's tests
package p type Config struct {	Spec struct {		Limits struct {tx0 reports: this anonymous struct is nested inside two others			CPU string		}		Name string	}} var table = []struct {	name string	want struct{ ok bool }}{}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/go:nested-anonymous-struct" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/go:nested-anonymous-struct] reason="..."

tx0 explain tx0/go:nested-anonymous-struct shows the same in the terminal. Found a false positive? Report it.