Skip to content

Commit

Permalink
mirror: linter that suggest using alternative string/[]byte functions (
Browse files Browse the repository at this point in the history
  • Loading branch information
butuzov authored Jun 1, 2023
1 parent 13de250 commit 9f2528a
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2140,6 +2140,7 @@ linters:
- maintidx
- makezero
- maligned
- mirror
- misspell
- musttag
- nakedret
Expand Down Expand Up @@ -2253,6 +2254,7 @@ linters:
- maintidx
- makezero
- maligned
- mirror
- misspell
- musttag
- nakedret
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
github.com/breml/bidichk v0.2.4
github.com/breml/errchkjson v0.3.1
github.com/butuzov/ireturn v0.2.0
github.com/butuzov/mirror v1.1.0
github.com/charithe/durationcheck v0.0.10
github.com/curioswitch/go-reassign v0.2.0
github.com/daixiang0/gci v0.10.1
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.

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

import (
"sync"

"github.com/butuzov/mirror"
"golang.org/x/tools/go/analysis"

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

func NewMirror() *goanalysis.Linter {
var (
mu sync.Mutex
issues []goanalysis.Issue
)

a := mirror.NewAnalyzer()
a.Run = func(pass *analysis.Pass) (any, error) {
// mirror only lints test files if the `--with-tests` flag is passed,
// so we pass the `with-tests` flag as true to the analyzer before running it.
// This can be turned off by using the regular golangci-lint flags such as `--tests` or `--skip-files`
// or can be disabled per linter via exclude rules.
// (see https:/golangci/golangci-lint/issues/2527#issuecomment-1023707262)
violations := mirror.Run(pass, true)

if len(violations) == 0 {
return nil, nil
}

for index := range violations {
i := violations[index].Issue(pass.Fset)

issue := result.Issue{
FromLinter: a.Name,
Text: i.Message,
Pos: i.Start,
}

if len(i.InlineFix) > 0 {
issue.Replacement = &result.Replacement{
Inline: &result.InlineFix{
StartCol: i.Start.Column - 1,
Length: len(i.Original),
NewString: i.InlineFix,
},
}
}

mu.Lock()
issues = append(issues, goanalysis.NewIssue(&issue, pass))
mu.Unlock()
}

return nil, nil
}

analyzer := goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
nil,
).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
return issues
}).WithLoadMode(goanalysis.LoadModeTypesInfo)

return analyzer
}
6 changes: 6 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithURL("https:/mdempsky/maligned").
Deprecated("The repository of the linter has been archived by the owner.", "v1.38.0", "govet 'fieldalignment'"),

linter.NewConfig(golinters.NewMirror()).
WithSince("v1.53.0").
WithPresets(linter.PresetStyle).
WithLoadForGoAnalysis().
WithURL("https:/butuzov/mirror"),

linter.NewConfig(golinters.NewMisspell(misspellCfg)).
WithSince("v1.8.0").
WithPresets(linter.PresetStyle, linter.PresetComment).
Expand Down
12 changes: 12 additions & 0 deletions test/testdata/mirror.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//golangcitest:args -Emirror
package testdata

import (
"strings"
"unicode/utf8"
)

func foobar() {
_ = utf8.RuneCount([]byte("foobar")) // want `avoid allocations with utf8\.RuneCountInString`
_ = strings.Compare(string([]byte{'f', 'o', 'o', 'b', 'a', 'r'}), string([]byte{'f', 'o', 'o', 'b', 'a', 'r'})) // want `avoid allocations with bytes\.Compare`
}

0 comments on commit 9f2528a

Please sign in to comment.