报告对返回形参地址(例如 IntPtr(x) 会返回 &x)且可被替换为 new(...) 的辅助函数的调用。
在 Go 1.26 中,new 可以直接接收值。 这样便可在不使用包装器函数的情况下创建指向字面量和其他值的指针。
示例:
type Config struct {
Retries *int
Label *string
}
func Default() Config {
return Config{
Retries: IntPtr(3),
Label: StringPtr("default"),
}
}
要修正代码,请使用将 foo(x) 简化为 new(x) 快速修复。
在应用快速修复后:
func Default() Config {
return Config{
Retries: new(3),
Label: new("default"),
}
}