Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2297,6 +2297,7 @@ linters:
- ireturn
- lll
- loggercheck
- loopvar
- maintidx
- makezero
- maligned
Expand Down Expand Up @@ -2417,6 +2418,7 @@ linters:
- ireturn
- lll
- loggercheck
- loopvar
- maintidx
- makezero
- maligned
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ require (
github.com/jingyugao/rowserrcheck v1.1.1
github.com/jirfag/go-printf-func-name v0.0.0-20200119135958-7558a9eaa5af
github.com/julz/importas v0.1.0
github.com/karamaru-alpha/loopvar v1.0.4
github.com/kisielk/errcheck v1.6.3
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/loopvar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package golinters

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

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

func NewLoopVar() *goanalysis.Linter {
a := loopvar.Analyzer

return goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
nil,
).WithLoadMode(goanalysis.LoadModeTypesInfo)
}
6 changes: 6 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithAlternativeNames("logrlint").
WithURL("https://github.com/timonwong/loggercheck"),

linter.NewConfig(golinters.NewLoopVar()).
WithSince("v1.56.0").
WithLoadForGoAnalysis().
WithPresets(linter.PresetStyle).
WithURL("https://github.com/karamaru-alpha/loopvar"),

linter.NewConfig(golinters.NewMaintIdx(maintIdxCfg)).
WithSince("v1.44.0").
WithPresets(linter.PresetComplexity).
Expand Down
38 changes: 38 additions & 0 deletions test/testdata/loopvar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//golangcitest:args -Eloopvar
package testdata

import (
"fmt"
)

func foo() {
slice := []int{1, 2, 3}
fns := make([]func(), 0, len(slice)*2)
for i, v := range slice {
i := i // want `The loop variable "i" should not be copied \(Go 1.22~ or Go 1.21 GOEXPERIMENT=loopvar\)`
fns = append(fns, func() {
fmt.Print(i)
})
_v := v // want `The loop variable "v" should not be copied \(Go 1.22~ or Go 1.21 GOEXPERIMENT=loopvar\)`
fns = append(fns, func() {
fmt.Print(_v)
})
}
for _, fn := range fns {
fn()
}
}

func bar() {
loopCount := 3
fns := make([]func(), 0, loopCount)
for i := 1; i <= loopCount; i++ {
i := i // want `The loop variable "i" should not be copied \(Go 1.22~ or Go 1.21 GOEXPERIMENT=loopvar\)`
fns = append(fns, func() {
fmt.Print(i)
})
}
for _, fn := range fns {
fn()
}
}