From 0ff046350a4648240f1ecbfa716f7695a995d2e1 Mon Sep 17 00:00:00 2001 From: Oleg Butuzov Date: Mon, 8 May 2023 10:14:03 +0300 Subject: [PATCH 1/9] mirror: linter that suggest using alternative string/[]byte functions --- go.mod | 1 + go.sum | 2 ++ pkg/golinters/mirror.go | 31 +++++++++++++++++++++++++++++++ pkg/lint/lintersdb/manager.go | 6 ++++++ test/testdata/mirror.go | 12 ++++++++++++ 5 files changed, 52 insertions(+) create mode 100644 pkg/golinters/mirror.go create mode 100644 test/testdata/mirror.go diff --git a/go.mod b/go.mod index b5e5132df472..483e3eb717a3 100644 --- a/go.mod +++ b/go.mod @@ -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 v0.1.0 github.com/charithe/durationcheck v0.0.10 github.com/curioswitch/go-reassign v0.2.0 github.com/daixiang0/gci v0.10.1 diff --git a/go.sum b/go.sum index 9c5f1683ca58..6a90d75482b6 100644 --- a/go.sum +++ b/go.sum @@ -94,6 +94,8 @@ github.com/breml/errchkjson v0.3.1 h1:hlIeXuspTyt8Y/UmP5qy1JocGNR00KQHgfaNtRAjox github.com/breml/errchkjson v0.3.1/go.mod h1:XroxrzKjdiutFyW3nWhw34VGg7kiMsDQox73yWCGI2U= github.com/butuzov/ireturn v0.2.0 h1:kCHi+YzC150GE98WFuZQu9yrTn6GEydO2AuPLbTgnO4= github.com/butuzov/ireturn v0.2.0/go.mod h1:Wh6Zl3IMtTpaIKbmwzqi6olnM9ptYQxxVacMsOEFPoc= +github.com/butuzov/mirror v0.1.0 h1:TtL2PcOVq1WA2WiZkEOD9yUBPZuywuoSLaWhAfz9emY= +github.com/butuzov/mirror v0.1.0/go.mod h1:ZIrw2ZwR/ST6ixQQ6r4Fbz9PMbGth0gjTCaM98tWn38= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= diff --git a/pkg/golinters/mirror.go b/pkg/golinters/mirror.go new file mode 100644 index 000000000000..81de121dc41c --- /dev/null +++ b/pkg/golinters/mirror.go @@ -0,0 +1,31 @@ +package golinters + +import ( + "github.com/butuzov/mirror" + "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" +) + +func NewMirror() *goanalysis.Linter { + a := mirror.NewAnalyzer() + + // 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) + + cfg := map[string]map[string]any{ + a.Name: { + "with-tests": true, + }, + } + + return goanalysis.NewLinter( + a.Name, + a.Doc, + []*analysis.Analyzer{a}, + cfg, + ).WithLoadMode(goanalysis.LoadModeWholeProgram) +} diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 3c2394a4c88c..17ce2d9cbdc1 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -654,6 +654,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithURL("https://github.com/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://github.com/butuzov/mirror"), + linter.NewConfig(golinters.NewMisspell(misspellCfg)). WithSince("v1.8.0"). WithPresets(linter.PresetStyle, linter.PresetComment). diff --git a/test/testdata/mirror.go b/test/testdata/mirror.go new file mode 100644 index 000000000000..92967aea47b7 --- /dev/null +++ b/test/testdata/mirror.go @@ -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` +} From 8791f637e9491881688c707de50de00a11b76c85 Mon Sep 17 00:00:00 2001 From: Oleg Butuzov Date: Mon, 8 May 2023 14:13:19 +0300 Subject: [PATCH 2/9] release: go mod bump to v0.1.1 fixed error in linter in go1.19 used --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 483e3eb717a3..b29b974a0bb3 100644 --- a/go.mod +++ b/go.mod @@ -24,7 +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 v0.1.0 + github.com/butuzov/mirror v0.1.1 github.com/charithe/durationcheck v0.0.10 github.com/curioswitch/go-reassign v0.2.0 github.com/daixiang0/gci v0.10.1 diff --git a/go.sum b/go.sum index 6a90d75482b6..c1cd2021b8b4 100644 --- a/go.sum +++ b/go.sum @@ -94,8 +94,8 @@ github.com/breml/errchkjson v0.3.1 h1:hlIeXuspTyt8Y/UmP5qy1JocGNR00KQHgfaNtRAjox github.com/breml/errchkjson v0.3.1/go.mod h1:XroxrzKjdiutFyW3nWhw34VGg7kiMsDQox73yWCGI2U= github.com/butuzov/ireturn v0.2.0 h1:kCHi+YzC150GE98WFuZQu9yrTn6GEydO2AuPLbTgnO4= github.com/butuzov/ireturn v0.2.0/go.mod h1:Wh6Zl3IMtTpaIKbmwzqi6olnM9ptYQxxVacMsOEFPoc= -github.com/butuzov/mirror v0.1.0 h1:TtL2PcOVq1WA2WiZkEOD9yUBPZuywuoSLaWhAfz9emY= -github.com/butuzov/mirror v0.1.0/go.mod h1:ZIrw2ZwR/ST6ixQQ6r4Fbz9PMbGth0gjTCaM98tWn38= +github.com/butuzov/mirror v0.1.1 h1:+CGTHPPEA/pQK64ljl2H1ilH2hhgd/AnroqFvaZHGak= +github.com/butuzov/mirror v0.1.1/go.mod h1:ZIrw2ZwR/ST6ixQQ6r4Fbz9PMbGth0gjTCaM98tWn38= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= From 7213bb295cf08ba2f1e563ea0c5afda7f1fef993 Mon Sep 17 00:00:00 2001 From: Oleg Butuzov Date: Mon, 8 May 2023 14:13:46 +0300 Subject: [PATCH 3/9] docs: added mirror to the enable/disable lists --- .golangci.reference.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.golangci.reference.yml b/.golangci.reference.yml index ea4cd43873f5..b759aa9d1c44 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -2140,6 +2140,7 @@ linters: - maintidx - makezero - maligned + - mirror - misspell - musttag - nakedret @@ -2253,6 +2254,7 @@ linters: - maintidx - makezero - maligned + - mirror - misspell - musttag - nakedret From c01275384115c7a61bacab3700442d45ab935b8c Mon Sep 17 00:00:00 2001 From: Oleg Butuzov Date: Mon, 8 May 2023 14:30:31 +0300 Subject: [PATCH 4/9] fix: v0.1.2 fixes a "failed release" with bad tag already propagated to go.sum --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index b29b974a0bb3..6febb9583dbd 100644 --- a/go.mod +++ b/go.mod @@ -24,7 +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 v0.1.1 + github.com/butuzov/mirror v0.1.2 github.com/charithe/durationcheck v0.0.10 github.com/curioswitch/go-reassign v0.2.0 github.com/daixiang0/gci v0.10.1 diff --git a/go.sum b/go.sum index c1cd2021b8b4..37ce2d82ce22 100644 --- a/go.sum +++ b/go.sum @@ -94,8 +94,8 @@ github.com/breml/errchkjson v0.3.1 h1:hlIeXuspTyt8Y/UmP5qy1JocGNR00KQHgfaNtRAjox github.com/breml/errchkjson v0.3.1/go.mod h1:XroxrzKjdiutFyW3nWhw34VGg7kiMsDQox73yWCGI2U= github.com/butuzov/ireturn v0.2.0 h1:kCHi+YzC150GE98WFuZQu9yrTn6GEydO2AuPLbTgnO4= github.com/butuzov/ireturn v0.2.0/go.mod h1:Wh6Zl3IMtTpaIKbmwzqi6olnM9ptYQxxVacMsOEFPoc= -github.com/butuzov/mirror v0.1.1 h1:+CGTHPPEA/pQK64ljl2H1ilH2hhgd/AnroqFvaZHGak= -github.com/butuzov/mirror v0.1.1/go.mod h1:ZIrw2ZwR/ST6ixQQ6r4Fbz9PMbGth0gjTCaM98tWn38= +github.com/butuzov/mirror v0.1.2 h1:AOFqNeGL+3znnsBptNI4PNKT1IpzTD+95vY7bsMM1pg= +github.com/butuzov/mirror v0.1.2/go.mod h1:8wpO6gP+57XaZ4BF6pFeZ8UwaXFxdjoaEObe710+8hM= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= From d0414bc843c6ae6baacd5d0169e2c3ab321d8995 Mon Sep 17 00:00:00 2001 From: Oleg Butuzov Date: Sat, 13 May 2023 14:37:24 +0300 Subject: [PATCH 5/9] chores: bump mirror version --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 6febb9583dbd..749fa025d8f8 100644 --- a/go.mod +++ b/go.mod @@ -24,7 +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 v0.1.2 + github.com/butuzov/mirror v0.2.0 github.com/charithe/durationcheck v0.0.10 github.com/curioswitch/go-reassign v0.2.0 github.com/daixiang0/gci v0.10.1 diff --git a/go.sum b/go.sum index 37ce2d82ce22..8e8120d219e5 100644 --- a/go.sum +++ b/go.sum @@ -94,8 +94,8 @@ github.com/breml/errchkjson v0.3.1 h1:hlIeXuspTyt8Y/UmP5qy1JocGNR00KQHgfaNtRAjox github.com/breml/errchkjson v0.3.1/go.mod h1:XroxrzKjdiutFyW3nWhw34VGg7kiMsDQox73yWCGI2U= github.com/butuzov/ireturn v0.2.0 h1:kCHi+YzC150GE98WFuZQu9yrTn6GEydO2AuPLbTgnO4= github.com/butuzov/ireturn v0.2.0/go.mod h1:Wh6Zl3IMtTpaIKbmwzqi6olnM9ptYQxxVacMsOEFPoc= -github.com/butuzov/mirror v0.1.2 h1:AOFqNeGL+3znnsBptNI4PNKT1IpzTD+95vY7bsMM1pg= -github.com/butuzov/mirror v0.1.2/go.mod h1:8wpO6gP+57XaZ4BF6pFeZ8UwaXFxdjoaEObe710+8hM= +github.com/butuzov/mirror v0.2.0 h1:U0QCGQXXij5ldKvHpYqedkVoCesmrMt4ZqKhA8eL6Iw= +github.com/butuzov/mirror v0.2.0/go.mod h1:8wpO6gP+57XaZ4BF6pFeZ8UwaXFxdjoaEObe710+8hM= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= From 7c701e289785d72218c782286d45a8c1a8eb6790 Mon Sep 17 00:00:00 2001 From: Oleg Butuzov Date: Sat, 13 May 2023 14:38:57 +0300 Subject: [PATCH 6/9] feature: custom issue reporter --- pkg/golinters/mirror.go | 63 +++++++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 12 deletions(-) diff --git a/pkg/golinters/mirror.go b/pkg/golinters/mirror.go index 81de121dc41c..d8197e592970 100644 --- a/pkg/golinters/mirror.go +++ b/pkg/golinters/mirror.go @@ -1,31 +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 ( // Issue reporter related. + 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) + withTests := true + violations := mirror.Run(pass, withTests) + + if len(violations) == 0 { + return nil, nil + } - // 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) + for i := range violations { + tmp := violations[i].Issue(pass) - cfg := map[string]map[string]any{ - a.Name: { - "with-tests": true, - }, + issue := result.Issue{ + FromLinter: a.Name, + Text: tmp.Message, + Pos: tmp.Start, + } + + if len(tmp.InlineFix) > 0 { + issue.Replacement = &result.Replacement{ + Inline: &result.InlineFix{ + StartCol: tmp.Start.Column - 1, + Length: len(tmp.Original), + NewString: tmp.InlineFix, + }, + } + } + mu.Lock() + issues = append(issues, goanalysis.NewIssue(&issue, pass)) + mu.Unlock() + } + + return nil, nil } - return goanalysis.NewLinter( + analyzer := goanalysis.NewLinter( a.Name, a.Doc, []*analysis.Analyzer{a}, - cfg, - ).WithLoadMode(goanalysis.LoadModeWholeProgram) + nil, + ).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { + return issues + }).WithLoadMode(goanalysis.LoadModeTypesInfo) + + return analyzer } From f3dc813498a04a45e0313b061612562cabbf8809 Mon Sep 17 00:00:00 2001 From: Oleg Butuzov Date: Mon, 15 May 2023 11:41:02 +0300 Subject: [PATCH 7/9] chores: version bump & code change --- go.mod | 2 +- go.sum | 4 ++-- pkg/golinters/mirror.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 749fa025d8f8..2245e8eedb7e 100644 --- a/go.mod +++ b/go.mod @@ -24,7 +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 v0.2.0 + github.com/butuzov/mirror v1.0.0 github.com/charithe/durationcheck v0.0.10 github.com/curioswitch/go-reassign v0.2.0 github.com/daixiang0/gci v0.10.1 diff --git a/go.sum b/go.sum index 8e8120d219e5..c4bc57587125 100644 --- a/go.sum +++ b/go.sum @@ -94,8 +94,8 @@ github.com/breml/errchkjson v0.3.1 h1:hlIeXuspTyt8Y/UmP5qy1JocGNR00KQHgfaNtRAjox github.com/breml/errchkjson v0.3.1/go.mod h1:XroxrzKjdiutFyW3nWhw34VGg7kiMsDQox73yWCGI2U= github.com/butuzov/ireturn v0.2.0 h1:kCHi+YzC150GE98WFuZQu9yrTn6GEydO2AuPLbTgnO4= github.com/butuzov/ireturn v0.2.0/go.mod h1:Wh6Zl3IMtTpaIKbmwzqi6olnM9ptYQxxVacMsOEFPoc= -github.com/butuzov/mirror v0.2.0 h1:U0QCGQXXij5ldKvHpYqedkVoCesmrMt4ZqKhA8eL6Iw= -github.com/butuzov/mirror v0.2.0/go.mod h1:8wpO6gP+57XaZ4BF6pFeZ8UwaXFxdjoaEObe710+8hM= +github.com/butuzov/mirror v1.0.0 h1:NMUfPjE+7WaY5Furw8jysolT/TF276Mxn746rxukJvA= +github.com/butuzov/mirror v1.0.0/go.mod h1:8wpO6gP+57XaZ4BF6pFeZ8UwaXFxdjoaEObe710+8hM= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= diff --git a/pkg/golinters/mirror.go b/pkg/golinters/mirror.go index d8197e592970..83f617631b89 100644 --- a/pkg/golinters/mirror.go +++ b/pkg/golinters/mirror.go @@ -32,7 +32,7 @@ func NewMirror() *goanalysis.Linter { } for i := range violations { - tmp := violations[i].Issue(pass) + tmp := violations[i].Issue(pass.Fset) issue := result.Issue{ FromLinter: a.Name, From cddbda76f6edebad7351439bbae54e3b62a502cd Mon Sep 17 00:00:00 2001 From: Oleg Butuzov Date: Thu, 1 Jun 2023 08:08:30 +0300 Subject: [PATCH 8/9] build: bump `mirror` to v1.1.0 --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 2245e8eedb7e..72e8ee3f6d5b 100644 --- a/go.mod +++ b/go.mod @@ -24,7 +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.0.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 diff --git a/go.sum b/go.sum index c4bc57587125..4f45e94408a3 100644 --- a/go.sum +++ b/go.sum @@ -94,8 +94,8 @@ github.com/breml/errchkjson v0.3.1 h1:hlIeXuspTyt8Y/UmP5qy1JocGNR00KQHgfaNtRAjox github.com/breml/errchkjson v0.3.1/go.mod h1:XroxrzKjdiutFyW3nWhw34VGg7kiMsDQox73yWCGI2U= github.com/butuzov/ireturn v0.2.0 h1:kCHi+YzC150GE98WFuZQu9yrTn6GEydO2AuPLbTgnO4= github.com/butuzov/ireturn v0.2.0/go.mod h1:Wh6Zl3IMtTpaIKbmwzqi6olnM9ptYQxxVacMsOEFPoc= -github.com/butuzov/mirror v1.0.0 h1:NMUfPjE+7WaY5Furw8jysolT/TF276Mxn746rxukJvA= -github.com/butuzov/mirror v1.0.0/go.mod h1:8wpO6gP+57XaZ4BF6pFeZ8UwaXFxdjoaEObe710+8hM= +github.com/butuzov/mirror v1.1.0 h1:ZqX54gBVMXu78QLoiqdwpl2mgmoOJTk7s4p4o+0avZI= +github.com/butuzov/mirror v1.1.0/go.mod h1:8Q0BdQU6rC6WILDiBM60DBfvV78OLJmMmixe7GF45AE= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= From 9b61a82fb834800ea2511f52f81d60a1ca1afce1 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Thu, 1 Jun 2023 11:14:18 +0200 Subject: [PATCH 9/9] review --- pkg/golinters/mirror.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/pkg/golinters/mirror.go b/pkg/golinters/mirror.go index 83f617631b89..4adc001a19e3 100644 --- a/pkg/golinters/mirror.go +++ b/pkg/golinters/mirror.go @@ -12,43 +12,43 @@ import ( ) func NewMirror() *goanalysis.Linter { - var ( // Issue reporter related. + 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) - withTests := true - violations := mirror.Run(pass, withTests) + // 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 i := range violations { - tmp := violations[i].Issue(pass.Fset) + for index := range violations { + i := violations[index].Issue(pass.Fset) issue := result.Issue{ FromLinter: a.Name, - Text: tmp.Message, - Pos: tmp.Start, + Text: i.Message, + Pos: i.Start, } - if len(tmp.InlineFix) > 0 { + if len(i.InlineFix) > 0 { issue.Replacement = &result.Replacement{ Inline: &result.InlineFix{ - StartCol: tmp.Start.Column - 1, - Length: len(tmp.Original), - NewString: tmp.InlineFix, + StartCol: i.Start.Column - 1, + Length: len(i.Original), + NewString: i.InlineFix, }, } } + mu.Lock() issues = append(issues, goanalysis.NewIssue(&issue, pass)) mu.Unlock()