A struct type keeps a context.Context in a field
What it finds
type Worker struct { ctx context.Context }: a context belongs to one call, and the context package documents that it is passed as the first parameter of each function that needs it. Kept in a struct, it outlives the call it came from, its cancellation applies to whatever later uses the struct, and readers of a method cannot see which context governs it.
The field's type must be context.Context, resolved through the imports of the file. Types that exist to carry a context, such as the options of a single call, are a deliberate use, so the rule is strict.
From the rule's source, context_field.rs.
package p import "context" type worker struct { ctx context.Contexttx0 reports: a context is kept in a struct field instead of being passed to each call name string} type options struct { context.Contexttx0 reports: a context is kept in a struct field instead of being passed to each call} func (w *worker) run(ctx context.Context) {} 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:
[rules]
"tx0/go:context-field" = "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:context-field] reason="..."tx0 explain tx0/go:context-field shows the same in the terminal. Found a false positive? Report it.