Skip to content

x/tools/go/analysis/passes/modernize: fmtappendf changes nil semantics of []byte conversion #77557

@findleyr

Description

@findleyr

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

Metadata

Metadata

Assignees

Labels

BugReportIssues describing a possible bug in the Go implementation.ToolsThis label describes issues relating to any tools in the x/tools repository.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions