Skip to content

Commit df70758

Browse files
build(deps): bump github.com/karamaru-alpha/copyloopvar from 1.0.4 to 1.0.8 (#4444)
1 parent b5c339f commit df70758

File tree

9 files changed

+75
-9
lines changed

9 files changed

+75
-9
lines changed

.golangci.reference.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ linters-settings:
149149
first-strong-isolate: false
150150
pop-directional-isolate: false
151151

152+
copyloopvar:
153+
# If true, ignore aliasing of loop variables.
154+
# Default: false
155+
ignore-alias: true
156+
152157
cyclop:
153158
# The maximal code complexity to report.
154159
# Default: 10

go.mod

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

go.sum

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/linters_settings.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ var defaultLintersSettings = LintersSettings{
206206
type LintersSettings struct {
207207
Asasalint AsasalintSettings
208208
BiDiChk BiDiChkSettings
209+
CopyLoopVar CopyLoopVarSettings
209210
Cyclop Cyclop
210211
Decorder DecorderSettings
211212
Depguard DepGuardSettings
@@ -313,6 +314,10 @@ type BiDiChkSettings struct {
313314
PopDirectionalIsolate bool `mapstructure:"pop-directional-isolate"`
314315
}
315316

317+
type CopyLoopVarSettings struct {
318+
IgnoreAlias bool `mapstructure:"ignore-alias"`
319+
}
320+
316321
type Cyclop struct {
317322
MaxComplexity int `mapstructure:"max-complexity"`
318323
PackageAverage float64 `mapstructure:"package-average"`

pkg/golinters/copyloopvar.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,26 @@ import (
44
"github.com/karamaru-alpha/copyloopvar"
55
"golang.org/x/tools/go/analysis"
66

7+
"github.com/golangci/golangci-lint/pkg/config"
78
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
89
)
910

10-
func NewCopyLoopVar() *goanalysis.Linter {
11-
a := copyloopvar.Analyzer
11+
func NewCopyLoopVar(settings *config.CopyLoopVarSettings) *goanalysis.Linter {
12+
a := copyloopvar.NewAnalyzer()
13+
14+
var cfg map[string]map[string]any
15+
if settings != nil {
16+
cfg = map[string]map[string]any{
17+
a.Name: {
18+
"ignore-alias": settings.IgnoreAlias,
19+
},
20+
}
21+
}
1222

1323
return goanalysis.NewLinter(
1424
a.Name,
1525
a.Doc,
1626
[]*analysis.Analyzer{a},
17-
nil,
27+
cfg,
1828
).WithLoadMode(goanalysis.LoadModeSyntax)
1929
}

pkg/lint/lintersdb/builder_linter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (b LinterBuilder) Build(cfg *config.Config) []*linter.Config {
6060
WithLoadForGoAnalysis().
6161
WithURL("https://github.com/kkHAIKE/contextcheck"),
6262

63-
linter.NewConfig(golinters.NewCopyLoopVar()).
63+
linter.NewConfig(golinters.NewCopyLoopVar(&cfg.LintersSettings.CopyLoopVar)).
6464
WithSince("v1.57.0").
6565
WithPresets(linter.PresetStyle).
6666
WithURL("https://github.com/karamaru-alpha/copyloopvar").

test/testdata/configs/copyloopvar.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
linters-settings:
2+
copyloopvar:
3+
ignore-alias: true

test/testdata/copyloopvar.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ func copyloopvarCase1() {
1313
fns = append(fns, func() {
1414
fmt.Println(i)
1515
})
16-
_v := v // want `The copy of the 'for' variable "v" can be deleted \(Go 1\.22\+\)`
16+
v := v // want `The copy of the 'for' variable "v" can be deleted \(Go 1\.22\+\)`
1717
fns = append(fns, func() {
18-
fmt.Println(_v)
18+
fmt.Println(v)
1919
})
20+
_v := v // want `The copy of the 'for' variable "v" can be deleted \(Go 1\.22\+\)`
21+
_ = _v
2022
}
2123
for _, fn := range fns {
2224
fn()

test/testdata/copyloopvar_custom.go

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

0 commit comments

Comments
 (0)