Skip to content

Commit 2417da1

Browse files
feat: add copyloopvar linter (#4382)
1 parent b96ff83 commit 2417da1

File tree

6 files changed

+65
-0
lines changed

6 files changed

+65
-0
lines changed

.golangci.reference.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2485,6 +2485,7 @@ linters:
24852485
- bodyclose
24862486
- containedctx
24872487
- contextcheck
2488+
- copyloopvar
24882489
- cyclop
24892490
- deadcode
24902491
- decorder
@@ -2607,6 +2608,7 @@ linters:
26072608
- bodyclose
26082609
- containedctx
26092610
- contextcheck
2611+
- copyloopvar
26102612
- cyclop
26112613
- deadcode
26122614
- decorder

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ require (
6161
github.com/jirfag/go-printf-func-name v0.0.0-20200119135958-7558a9eaa5af
6262
github.com/jjti/go-spancheck v0.5.2
6363
github.com/julz/importas v0.1.0
64+
github.com/karamaru-alpha/copyloopvar v1.0.4
6465
github.com/kisielk/errcheck v1.7.0
6566
github.com/kkHAIKE/contextcheck v1.1.4
6667
github.com/kulti/thelper v0.6.3

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/copyloopvar.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package golinters
2+
3+
import (
4+
"github.com/karamaru-alpha/copyloopvar"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+
)
9+
10+
func NewCopyLoopVar() *goanalysis.Linter {
11+
a := copyloopvar.Analyzer
12+
13+
return goanalysis.NewLinter(
14+
a.Name,
15+
a.Doc,
16+
[]*analysis.Analyzer{a},
17+
nil,
18+
).WithLoadMode(goanalysis.LoadModeSyntax)
19+
}

pkg/lint/lintersdb/manager.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
298298
WithLoadForGoAnalysis().
299299
WithURL("https://github.com/kkHAIKE/contextcheck"),
300300

301+
linter.NewConfig(golinters.NewCopyLoopVar()).
302+
WithSince("v1.57.0").
303+
WithPresets(linter.PresetStyle).
304+
WithURL("https://github.com/karamaru-alpha/copyloopvar"),
305+
301306
linter.NewConfig(golinters.NewCyclop(cyclopCfg)).
302307
WithSince("v1.37.0").
303308
WithLoadForGoAnalysis().

test/testdata/copyloopvar.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//golangcitest:args -Ecopyloopvar
2+
package testdata
3+
4+
import "fmt"
5+
6+
func copyloopvarCase1() {
7+
slice := []int{1, 2, 3}
8+
fns := make([]func(), 0, len(slice)*2)
9+
for i, v := range slice {
10+
i := i // want `The copy of the 'for' variable "i" can be deleted \(Go 1\.22\+\)`
11+
fns = append(fns, func() {
12+
fmt.Println(i)
13+
})
14+
_v := v // want `The copy of the 'for' variable "v" can be deleted \(Go 1\.22\+\)`
15+
fns = append(fns, func() {
16+
fmt.Println(_v)
17+
})
18+
}
19+
for _, fn := range fns {
20+
fn()
21+
}
22+
}
23+
24+
func copyloopvarCase2() {
25+
loopCount := 3
26+
fns := make([]func(), 0, loopCount)
27+
for i := 1; i <= loopCount; i++ {
28+
i := i // want `The copy of the 'for' variable "i" can be deleted \(Go 1\.22\+\)`
29+
fns = append(fns, func() {
30+
fmt.Println(i)
31+
})
32+
}
33+
for _, fn := range fns {
34+
fn()
35+
}
36+
}

0 commit comments

Comments
 (0)