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

Add perfsprint linter #3714

Merged
merged 5 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2301,6 +2301,7 @@ linters:
- nosnakecase
- nosprintfhostport
- paralleltest
- perfsprint
- prealloc
- predeclared
- promlinter
Expand Down Expand Up @@ -2420,6 +2421,7 @@ linters:
- nosnakecase
- nosprintfhostport
- paralleltest
- perfsprint
- prealloc
- predeclared
- promlinter
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
github.com/breml/errchkjson v0.3.6
github.com/butuzov/ireturn v0.2.1
github.com/butuzov/mirror v1.1.0
github.com/catenacyber/perfsprint v0.2.0
github.com/charithe/durationcheck v0.0.10
github.com/curioswitch/go-reassign v0.2.0
github.com/daixiang0/gci v0.11.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions pkg/golinters/perfsprint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package golinters

import (
"github.com/catenacyber/perfsprint/analyzer"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

func NewGoStrconv() *goanalysis.Linter {
return goanalysis.NewLinter(
"perfsprint",
"Checks usages of `fmt.Sprintf` which have faster alternatives.",
[]*analysis.Analyzer{analyzer.Analyzer},
nil,
).WithLoadMode(goanalysis.LoadModeSyntax)
}
5 changes: 5 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetStyle, linter.PresetTest).
WithURL("https://github.com/kunwardeep/paralleltest"),

linter.NewConfig(golinters.NewPerfSprint()).
WithSince("v1.54.2").
WithPresets(linter.PresetStyle).
catenacyber marked this conversation as resolved.
Show resolved Hide resolved
WithURL("https://github.com/catenacyber/perfsprint"),

linter.NewConfig(golinters.NewPreAlloc(preallocCfg)).
WithSince("v1.19.0").
WithPresets(linter.PresetPerformance).
Expand Down
8 changes: 8 additions & 0 deletions test/testdata/perfsprint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//golangcitest:args -Eperfsprint
package testdata

import "fmt"

func SprintfCouldBeStrconv() {
fmt.Sprintf("%d", 42) // want "Sprintf can be replaced with faster strconv.Itoa"
Antonboom marked this conversation as resolved.
Show resolved Hide resolved
}