-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mirror: linter that suggest using alternative string/[]byte functions (…
- Loading branch information
Showing
6 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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://github.com/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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | ||
} |