Skip to content

Commit f9c8e6d

Browse files
committed
mirror: linter that suggest using alternative string/[]byte functions
1 parent d42f373 commit f9c8e6d

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ require (
2424
github.com/breml/bidichk v0.2.4
2525
github.com/breml/errchkjson v0.3.1
2626
github.com/butuzov/ireturn v0.2.0
27+
github.com/butuzov/mirror v0.1.0
2728
github.com/charithe/durationcheck v0.0.10
2829
github.com/curioswitch/go-reassign v0.2.0
2930
github.com/daixiang0/gci v0.10.1

go.sum

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/mirror.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package golinters
2+
3+
import (
4+
"github.com/butuzov/mirror"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+
)
9+
10+
func NewMirror() *goanalysis.Linter {
11+
a := mirror.NewAnalyzer()
12+
13+
// mirror only lints test files if the `--with-tests` flag is passed, so we
14+
// pass the `with-tests` flag as true to the analyzer before running it. This
15+
// can be turned off by using the regular golangci-lint flags such as `--tests`
16+
// or `--skip-files` or can be disabled per linter via exclude rules (see
17+
// https://github.com/golangci/golangci-lint/issues/2527#issuecomment-1023707262)
18+
19+
cfg := map[string]map[string]any{
20+
a.Name: {
21+
"with-tests": true,
22+
},
23+
}
24+
25+
return goanalysis.NewLinter(
26+
a.Name,
27+
a.Doc,
28+
[]*analysis.Analyzer{a},
29+
cfg,
30+
).WithLoadMode(goanalysis.LoadModeWholeProgram)
31+
}

pkg/lint/lintersdb/manager.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
650650
WithURL("https://github.com/mdempsky/maligned").
651651
Deprecated("The repository of the linter has been archived by the owner.", "v1.38.0", "govet 'fieldalignment'"),
652652

653+
linter.NewConfig(golinters.NewMirror()).
654+
WithSince("v1.53.0").
655+
WithPresets(linter.PresetStyle).
656+
WithLoadForGoAnalysis().
657+
WithURL("https://github.com/butuzov/mirror"),
658+
653659
linter.NewConfig(golinters.NewMisspell(misspellCfg)).
654660
WithSince("v1.8.0").
655661
WithPresets(linter.PresetStyle, linter.PresetComment).

test/testdata/mirror.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//golangcitest:args -Emirror
2+
package testdata
3+
4+
import (
5+
"strings"
6+
"unicode/utf8"
7+
)
8+
9+
func foobar() {
10+
_ = utf8.RuneCount([]byte("foobar")) // want `avoid allocations with utf8\.RuneCountInString`
11+
_ = strings.Compare(string([]byte{'f', 'o', 'o', 'b', 'a', 'r'}), string([]byte{'f', 'o', 'o', 'b', 'a', 'r'})) // want `avoid allocations with bytes\.Compare`
12+
}

0 commit comments

Comments
 (0)