Inspectopedia 2025.3 Help

Type-safe error unwrapping with 'errors.AsType'

Syntax update: type-safe error unwrapping with errors.AsType

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.

Example:

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.

Locating this inspection

By ID

Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.

GoErrorsAsToAsType
Via Settings dialog

Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.

Settings or Preferences | Editor | Inspections | Go | Syntax updates

Inspection ID: GoErrorsAsToAsType

Suppressing Inspection

You can suppress this inspection by placing the following comment marker before the code fragment where you no longer want messages from this inspection to appear:

//noinspection GoErrorsAsToAsType

More detailed instructions as well as other ways and options that you have can be found in the product documentation:

Inspection Details

By default bundled with:

GoLand 2025.3, Qodana for Go 2025.3,

Last modified: 04 March 2026