报告可被替换为 errors.AsType 的对 errors.As 的调用。

在 Go 1.26 及更高版本中,errors.AsType[T] 会直接返回匹配的错误值。 这将移除单独声明的变量和传递给 errors.As 的指针。

示例:


func Describe(err error) string {
	var pe *PathError
	if errors.As(err, &pe) {
		return fmt.Sprintf("path error during %s", pe.Op)
	}
	return "other"
}

要修正代码,请使用将 errors.As 替换为 AsType[*PathError] 快速修复。

在应用快速修复后:


func Describe(err error) string {
	if pe, ok := errors.AsType[*PathError](err); ok {
		return fmt.Sprintf("path error during %s", pe.Op)
	}
	return "other"
}