Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit arg index #17

Merged
merged 3 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
// Probably unreachable.
return
}
// one single explicit arg is simplified
if strings.HasPrefix(verb, "%[1]") {
verb = "%" + verb[4:]
}

fn = "fmt.Sprintf"
value = call.Args[1]
Expand Down Expand Up @@ -478,7 +482,6 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
}

if d != nil {
// Need to run goimports to fix using of fmt, strconv, errors or encoding/hex afterwards.
pass.Report(*d)
}
})
Expand Down
1 change: 1 addition & 0 deletions analyzer/testdata/src/p/p.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func positive() {
fmt.Sprintf("hello") // want "fmt.Sprintf can be replaced with just using the string"
fmt.Sprint("hello") // want "fmt.Sprint can be replaced with just using the string"
fmt.Sprintf("%s", s) // want "fmt.Sprintf can be replaced with just using the string"
fmt.Sprintf("%[1]s", s) // want "fmt.Sprintf can be replaced with just using the string"
fmt.Sprintf("%v", s) // want "fmt.Sprintf can be replaced with just using the string"
fmt.Sprint(s) // want "fmt.Sprint can be replaced with just using the string"
fmt.Errorf("hello") // want "fmt.Errorf can be replaced with errors.New"
Expand Down
1 change: 1 addition & 0 deletions analyzer/testdata/src/p/p.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func positive() {
"hello" // want "fmt.Sprint can be replaced with just using the string"
s // want "fmt.Sprintf can be replaced with just using the string"
s // want "fmt.Sprintf can be replaced with just using the string"
s // want "fmt.Sprintf can be replaced with just using the string"
s // want "fmt.Sprint can be replaced with just using the string"
errors.New("hello") // want "fmt.Errorf can be replaced with errors.New"

Expand Down
Loading