Skip to content

Commit de1c391

Browse files
catenacyberldezAntonboom
authored
Add perfsprint linter (#3714)
Co-authored-by: Fernandez Ludovic <[email protected]> Co-authored-by: Anton Telyshev <[email protected]>
1 parent 1bfcc5f commit de1c391

File tree

6 files changed

+79
-0
lines changed

6 files changed

+79
-0
lines changed

Diff for: .golangci.reference.yml

+2
Original file line numberDiff line numberDiff line change
@@ -2314,6 +2314,7 @@ linters:
23142314
- nosnakecase
23152315
- nosprintfhostport
23162316
- paralleltest
2317+
- perfsprint
23172318
- prealloc
23182319
- predeclared
23192320
- promlinter
@@ -2433,6 +2434,7 @@ linters:
24332434
- nosnakecase
24342435
- nosprintfhostport
24352436
- paralleltest
2437+
- perfsprint
24362438
- prealloc
24372439
- predeclared
24382440
- promlinter

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ require (
2727
github.com/breml/errchkjson v0.3.6
2828
github.com/butuzov/ireturn v0.2.1
2929
github.com/butuzov/mirror v1.1.0
30+
github.com/catenacyber/perfsprint v0.2.0
3031
github.com/charithe/durationcheck v0.0.10
3132
github.com/curioswitch/go-reassign v0.2.0
3233
github.com/daixiang0/gci v0.11.2

Diff for: go.sum

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: pkg/golinters/perfsprint.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package golinters
2+
3+
import (
4+
"github.com/catenacyber/perfsprint/analyzer"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+
)
9+
10+
func NewPerfSprint() *goanalysis.Linter {
11+
a := analyzer.Analyzer
12+
13+
return goanalysis.NewLinter(
14+
a.Name,
15+
a.Doc,
16+
[]*analysis.Analyzer{a},
17+
nil,
18+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
19+
}

Diff for: pkg/lint/lintersdb/manager.go

+6
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
712712
WithPresets(linter.PresetStyle, linter.PresetTest).
713713
WithURL("https://github.com/kunwardeep/paralleltest"),
714714

715+
linter.NewConfig(golinters.NewPerfSprint()).
716+
WithSince("v1.55.0").
717+
WithLoadForGoAnalysis().
718+
WithPresets(linter.PresetPerformance).
719+
WithURL("https://github.com/catenacyber/perfsprint"),
720+
715721
linter.NewConfig(golinters.NewPreAlloc(preallocCfg)).
716722
WithSince("v1.19.0").
717723
WithPresets(linter.PresetPerformance).

Diff for: test/testdata/perfsprint.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//golangcitest:args -Eperfsprint
2+
package testdata
3+
4+
import "fmt"
5+
6+
func TestPerfsprint() {
7+
var (
8+
s string
9+
err error
10+
b bool
11+
i int
12+
i64 int64
13+
ui uint
14+
)
15+
16+
fmt.Sprintf("%s", s) // want "fmt.Sprintf can be replaced with just using the string"
17+
fmt.Sprint(s) // want "fmt.Sprint can be replaced with just using the string"
18+
fmt.Sprintf("%s", err) // want "fmt.Sprintf can be replaced with err.Error()"
19+
fmt.Sprint(err) // want "fmt.Sprint can be replaced with err.Error()"
20+
fmt.Sprintf("%t", b) // want "fmt.Sprintf can be replaced with faster strconv.FormatBool"
21+
fmt.Sprint(b) // want "fmt.Sprint can be replaced with faster strconv.FormatBool"
22+
fmt.Sprintf("%d", i) // want "fmt.Sprintf can be replaced with faster strconv.Itoa"
23+
fmt.Sprint(i) // want "fmt.Sprint can be replaced with faster strconv.Itoa"
24+
fmt.Sprintf("%d", i64) // want "fmt.Sprintf can be replaced with faster strconv.FormatInt"
25+
fmt.Sprint(i64) // want "fmt.Sprint can be replaced with faster strconv.FormatInt"
26+
fmt.Sprintf("%d", ui) // want "fmt.Sprintf can be replaced with faster strconv.FormatUint"
27+
fmt.Sprint(ui) // want "fmt.Sprint can be replaced with faster strconv.FormatUint"
28+
fmt.Sprintf("%x", []byte{'a'}) // want "fmt.Sprintf can be replaced with faster hex.EncodeToString"
29+
30+
fmt.Sprint("test", 42)
31+
fmt.Sprint(42, 42)
32+
fmt.Sprintf("test")
33+
fmt.Sprintf("%v")
34+
fmt.Sprintf("%d")
35+
fmt.Sprintf("%d", 42, 42)
36+
fmt.Sprintf("%#d", 42)
37+
fmt.Sprintf("value %d", 42)
38+
fmt.Sprintf("val%d", 42)
39+
fmt.Sprintf("%s %v", "hello", "world")
40+
fmt.Sprintf("%#v", 42)
41+
fmt.Sprintf("%T", struct{ string }{})
42+
fmt.Sprintf("%%v", 42)
43+
fmt.Sprintf("%3d", 42)
44+
fmt.Sprintf("% d", 42)
45+
fmt.Sprintf("%-10d", 42)
46+
fmt.Sprintf("%[2]d %[1]d\n", 11, 22)
47+
fmt.Sprintf("%[3]*.[2]*[1]f", 12.0, 2, 6)
48+
fmt.Sprintf("%d %d %#[1]x %#x", 16, 17)
49+
}

0 commit comments

Comments
 (0)