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

analyzer: use word concatenation rather than addition #24

Merged
merged 1 commit into from
Feb 14, 2024
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
4 changes: 2 additions & 2 deletions analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,10 +473,10 @@ func (n *perfSprint) run(pass *analysis.Pass) (interface{}, error) {
d = &analysis.Diagnostic{
Pos: call.Pos(),
End: call.End(),
Message: fn + " can be replaced with string addition",
Message: fn + " can be replaced with string concatenation",
SuggestedFixes: []analysis.SuggestedFix{
{
Message: "Use string addition",
Message: "Use string concatenation",
TextEdits: []analysis.TextEdit{{
Pos: call.Pos(),
End: call.End(),
Expand Down
2 changes: 1 addition & 1 deletion analyzer/replacements_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func BenchmarkStringAdditionFormatting(b *testing.B) {
b.ReportAllocs()
})

b.Run("string addition", func(b *testing.B) {
b.Run("string concatenation", func(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = "Hello " + "world"
}
Expand Down
6 changes: 3 additions & 3 deletions analyzer/testdata/src/p/p.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func positive() {
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"

fmt.Sprintf("Hello %s", s) // want "fmt.Sprintf can be replaced with string addition"
fmt.Sprintf("%s says Hello", s) // want "fmt.Sprintf can be replaced with string addition"
fmt.Sprintf("Hello says %[1]s", s) // want "fmt.Sprintf can be replaced with string addition"
fmt.Sprintf("Hello %s", s) // want "fmt.Sprintf can be replaced with string concatenation"
fmt.Sprintf("%s says Hello", s) // want "fmt.Sprintf can be replaced with string concatenation"
fmt.Sprintf("Hello says %[1]s", s) // want "fmt.Sprintf can be replaced with string concatenation"

var err error
fmt.Sprintf("%s", errSentinel) // want "fmt.Sprintf can be replaced with errSentinel.Error()"
Expand Down
6 changes: 3 additions & 3 deletions analyzer/testdata/src/p/p.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ func positive() {
s // want "fmt.Sprint can be replaced with just using the string"
errors.New("hello") // want "fmt.Errorf can be replaced with errors.New"

"Hello " + s // want "fmt.Sprintf can be replaced with string addition"
s + " says Hello" // want "fmt.Sprintf can be replaced with string addition"
"Hello says " + s // want "fmt.Sprintf can be replaced with string addition"
"Hello " + s // want "fmt.Sprintf can be replaced with string concatenation"
s + " says Hello" // want "fmt.Sprintf can be replaced with string concatenation"
"Hello says " + s // want "fmt.Sprintf can be replaced with string concatenation"

var err error
errSentinel.Error() // want "fmt.Sprintf can be replaced with errSentinel.Error()"
Expand Down
Loading