Skip to content

Commit

Permalink
build(deps): bump github.com/karamaru-alpha/copyloopvar from 1.0.4 to…
Browse files Browse the repository at this point in the history
… 1.0.8 (#4444)
  • Loading branch information
karamaru-alpha committed Mar 3, 2024
1 parent b5c339f commit df70758
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ linters-settings:
first-strong-isolate: false
pop-directional-isolate: false

copyloopvar:
# If true, ignore aliasing of loop variables.
# Default: false
ignore-alias: true

cyclop:
# The maximal code complexity to report.
# Default: 10
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ require (
github.com/jirfag/go-printf-func-name v0.0.0-20200119135958-7558a9eaa5af
github.com/jjti/go-spancheck v0.5.3
github.com/julz/importas v0.1.0
github.com/karamaru-alpha/copyloopvar v1.0.4
github.com/karamaru-alpha/copyloopvar v1.0.8
github.com/kisielk/errcheck v1.7.0
github.com/kkHAIKE/contextcheck v1.1.4
github.com/kulti/thelper v0.6.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ var defaultLintersSettings = LintersSettings{
type LintersSettings struct {
Asasalint AsasalintSettings
BiDiChk BiDiChkSettings
CopyLoopVar CopyLoopVarSettings
Cyclop Cyclop
Decorder DecorderSettings
Depguard DepGuardSettings
Expand Down Expand Up @@ -313,6 +314,10 @@ type BiDiChkSettings struct {
PopDirectionalIsolate bool `mapstructure:"pop-directional-isolate"`
}

type CopyLoopVarSettings struct {
IgnoreAlias bool `mapstructure:"ignore-alias"`
}

type Cyclop struct {
MaxComplexity int `mapstructure:"max-complexity"`
PackageAverage float64 `mapstructure:"package-average"`
Expand Down
16 changes: 13 additions & 3 deletions pkg/golinters/copyloopvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@ import (
"github.com/karamaru-alpha/copyloopvar"
"golang.org/x/tools/go/analysis"

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

func NewCopyLoopVar() *goanalysis.Linter {
a := copyloopvar.Analyzer
func NewCopyLoopVar(settings *config.CopyLoopVarSettings) *goanalysis.Linter {
a := copyloopvar.NewAnalyzer()

var cfg map[string]map[string]any
if settings != nil {
cfg = map[string]map[string]any{
a.Name: {
"ignore-alias": settings.IgnoreAlias,
},
}
}

return goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
nil,
cfg,
).WithLoadMode(goanalysis.LoadModeSyntax)
}
2 changes: 1 addition & 1 deletion pkg/lint/lintersdb/builder_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (b LinterBuilder) Build(cfg *config.Config) []*linter.Config {
WithLoadForGoAnalysis().
WithURL("https://github.com/kkHAIKE/contextcheck"),

linter.NewConfig(golinters.NewCopyLoopVar()).
linter.NewConfig(golinters.NewCopyLoopVar(&cfg.LintersSettings.CopyLoopVar)).
WithSince("v1.57.0").
WithPresets(linter.PresetStyle).
WithURL("https://github.com/karamaru-alpha/copyloopvar").
Expand Down
3 changes: 3 additions & 0 deletions test/testdata/configs/copyloopvar.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
linters-settings:
copyloopvar:
ignore-alias: true
6 changes: 4 additions & 2 deletions test/testdata/copyloopvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ func copyloopvarCase1() {
fns = append(fns, func() {
fmt.Println(i)
})
_v := v // want `The copy of the 'for' variable "v" can be deleted \(Go 1\.22\+\)`
v := v // want `The copy of the 'for' variable "v" can be deleted \(Go 1\.22\+\)`
fns = append(fns, func() {
fmt.Println(_v)
fmt.Println(v)
})
_v := v // want `The copy of the 'for' variable "v" can be deleted \(Go 1\.22\+\)`
_ = _v
}
for _, fn := range fns {
fn()
Expand Down
41 changes: 41 additions & 0 deletions test/testdata/copyloopvar_custom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//go:build go1.22

//golangcitest:args -Ecopyloopvar
//golangcitest:config_path testdata/configs/copyloopvar.yml
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)
})
_v := v
_ = _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 df70758

Please sign in to comment.