Skip to content

Commit

Permalink
feat: add copyloopvar linter (#4382)
Browse files Browse the repository at this point in the history
  • Loading branch information
karamaru-alpha authored Feb 15, 2024
1 parent b96ff83 commit 2417da1
Show file tree
Hide file tree
Showing 6 changed files with 65 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 @@ -2485,6 +2485,7 @@ linters:
- bodyclose
- containedctx
- contextcheck
- copyloopvar
- cyclop
- deadcode
- decorder
Expand Down Expand Up @@ -2607,6 +2608,7 @@ linters:
- bodyclose
- containedctx
- contextcheck
- copyloopvar
- cyclop
- deadcode
- decorder
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ require (
github.com/jirfag/go-printf-func-name v0.0.0-20200119135958-7558a9eaa5af
github.com/jjti/go-spancheck v0.5.2
github.com/julz/importas v0.1.0
github.com/karamaru-alpha/copyloopvar v1.0.4
github.com/kisielk/errcheck v1.7.0
github.com/kkHAIKE/contextcheck v1.1.4
github.com/kulti/thelper v0.6.3
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.

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

import (
"github.com/karamaru-alpha/copyloopvar"
"golang.org/x/tools/go/analysis"

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

func NewCopyLoopVar() *goanalysis.Linter {
a := copyloopvar.Analyzer

return goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
nil,
).WithLoadMode(goanalysis.LoadModeSyntax)
}
5 changes: 5 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithLoadForGoAnalysis().
WithURL("https://github.com/kkHAIKE/contextcheck"),

linter.NewConfig(golinters.NewCopyLoopVar()).
WithSince("v1.57.0").
WithPresets(linter.PresetStyle).
WithURL("https://github.com/karamaru-alpha/copyloopvar"),

linter.NewConfig(golinters.NewCyclop(cyclopCfg)).
WithSince("v1.37.0").
WithLoadForGoAnalysis().
Expand Down
36 changes: 36 additions & 0 deletions test/testdata/copyloopvar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//golangcitest:args -Ecopyloopvar
package testdata

import "fmt"

func copyloopvarCase1() {
slice := []int{1, 2, 3}
fns := make([]func(), 0, len(slice)*2)
for i, v := range slice {
i := i // want `The copy of the 'for' variable "i" can be deleted \(Go 1\.22\+\)`
fns = append(fns, func() {
fmt.Println(i)
})
_v := v // want `The copy of the 'for' variable "v" can be deleted \(Go 1\.22\+\)`
fns = append(fns, func() {
fmt.Println(_v)
})
}
for _, fn := range fns {
fn()
}
}

func copyloopvarCase2() {
loopCount := 3
fns := make([]func(), 0, loopCount)
for i := 1; i <= loopCount; i++ {
i := i // want `The copy of the 'for' variable "i" can be deleted \(Go 1\.22\+\)`
fns = append(fns, func() {
fmt.Println(i)
})
}
for _, fn := range fns {
fn()
}
}

0 comments on commit 2417da1

Please sign in to comment.