Skip to content

Commit

Permalink
analyzer: use word concatenation rather than addition
Browse files Browse the repository at this point in the history
  • Loading branch information
catenacyber committed Feb 13, 2024
1 parent b66013f commit c4a452a
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
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

0 comments on commit c4a452a

Please sign in to comment.