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:
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:
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.
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:
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: |