Reports usages of errors.As that can be replaced with errors.AsType, a generic function introduced in Go 1.26 that unwraps errors in a type-safe way and returns a typed result directly.
Instead of declaring a separate variable and passing a pointer to it, errors.AsType returns a typed result directly. This makes the code shorter, easier to read, and avoids common mistakes related to incorrect pointer usage.
예:
var cfgErr ConfigError // wrong: value, not pointer
if errors.As(err, &cfgErr) { // panic
fmt.Println("Config error on field:", cfgErr.Field)
} else {
fmt.Println("errors.As: could not detect ConfigError")
}
Here, errors.As requires a predeclared variable and a pointer to that variable.
To simplify the code and improve type safety, use the Replace with 'errors.AsType' quick-fix.
After the quick‑fix is applied:
if cfgErr, ok := errors.AsType[*ConfigError](err); ok {
fmt.Println("Config error on field:", cfgErr.Field)
} else {
fmt.Println("errors.As: could not detect ConfigError")
}
This form unwraps the error in a single expression, returns a correctly typed value, and removes the need for manual pointer handling.