A context value is stored under a key of a predeclared type, which other packages can collide with

tx0/go:context-key-basic-typerecommendedwarningbug

What it finds

context.WithValue(ctx, "user", u): values are looked up by comparing keys, and a key of a predeclared type such as string or int equals the same key that any other package uses, so two packages can read and overwrite each other's values. A key of an unexported type of the package, such as type userKey struct{}, cannot collide.

Reported are keys written as a string, number or rune literal, and constants of the file declared without a type whose value is such a literal; a constant declared with a type of its own is fine. context is resolved through the imports of the file.

From the rule's source, context_key_basic_type.rs.

Example from the rule's tests
package p import "context" const requestID = "request-id" type ctxKey string const typedKey ctxKey = "user" type userKey struct{} func f(ctx context.Context) context.Context {	ctx = context.WithValue(ctx, "user", 1)tx0 reports: ` has a predeclared type, so other packages can use the same key	ctx = context.WithValue(ctx, requestID, "x")tx0 reports: the key requestID has a predeclared type, so other packages can use the same key	ctx = context.WithValue(ctx, typedKey, 2)	ctx = context.WithValue(ctx, userKey{}, 3)	return ctx}
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 recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/go:context-key-basic-type" = "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-key-basic-type] reason="..."

tx0 explain tx0/go:context-key-basic-type shows the same in the terminal. Found a false positive? Report it.