-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Closed
Labels
BugReportIssues describing a possible bug in the Go implementation.Issues describing a possible bug in the Go implementation.ToolsThis label describes issues relating to any tools in the x/tools repository.This label describes issues relating to any tools in the x/tools repository.
Milestone
Description
The fmtappendf modernizer rewrites []byte(fmt.Sprintf(...)) to fmt.Appendf(nil, ...), but the two have different nil semantics when the format produces an empty string.
[]byte(fmt.Sprintf("")) returns an empty but non-nil []byte{}, while fmt.Appendf(nil, "") returns nil.
Before (go fix):
package main
import "fmt"
func main() {
b := []byte(fmt.Sprintf(""))
fmt.Println(b == nil)
}Output: false
After (GOTOOLCHAIN=go1.26.0 go fix):
package main
import "fmt"
func main() {
b := fmt.Appendf(nil, "")
fmt.Println(b == nil)
}Output: true
This can affect code that distinguishes between nil and empty slices (e.g., JSON marshaling, nil checks in tests).
CC @adonovan
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
BugReportIssues describing a possible bug in the Go implementation.Issues describing a possible bug in the Go implementation.ToolsThis label describes issues relating to any tools in the x/tools repository.This label describes issues relating to any tools in the x/tools repository.