Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: add copyloopvar linter #4382

Merged
merged 7 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -2485,6 +2485,7 @@ linters:
- bodyclose
- containedctx
- contextcheck
- copyloopvar
- cyclop
- deadcode
- decorder
Expand Down Expand Up @@ -2606,6 +2607,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 @@ -60,6 +60,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.3
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 `It's unnecessary to copy the loop variable "i"`
fns = append(fns, func() {
fmt.Println(i)
})
_v := v // want `It's unnecessary to copy the loop variable "v"`
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 `It's unnecessary to copy the loop variable "i"`
fns = append(fns, func() {
fmt.Println(i)
})
}
for _, fn := range fns {
fn()
}
}
Loading