Skip to content

Commit 1d7b6c3

Browse files
committed
fix #1404: integrate looppointer
1 parent 9cb902c commit 1d7b6c3

File tree

5 files changed

+65
-0
lines changed

5 files changed

+65
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ require (
4242
github.com/kulti/thelper v0.4.0
4343
github.com/kunwardeep/paralleltest v1.0.2
4444
github.com/kyoh86/exportloopref v0.1.8
45+
github.com/kyoh86/looppointer v0.1.7
4546
github.com/ldez/gomoddirectives v0.2.1
4647
github.com/ldez/tagliatelle v0.2.0
4748
github.com/maratori/testpackage v1.0.1

go.sum

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

pkg/golinters/looppointer.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package golinters
2+
3+
import (
4+
"github.com/kyoh86/looppointer"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+
)
9+
10+
// NewLoopPointer returns a new instance of Linter based on
11+
// github.com/kyoh86/looppointer.
12+
func NewLoopPointer() *goanalysis.Linter {
13+
analyzer := looppointer.Analyzer
14+
15+
return goanalysis.NewLinter(
16+
analyzer.Name,
17+
analyzer.Doc,
18+
[]*analysis.Analyzer{analyzer},
19+
nil,
20+
).WithLoadMode(goanalysis.LoadModeTypesInfo)
21+
}

pkg/lint/lintersdb/manager.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
488488
WithSince("v1.40.0").
489489
WithPresets(linter.PresetStyle).
490490
WithURL("https://github.com/ldez/tagliatelle"),
491+
linter.NewConfig(golinters.NewLoopPointer()).
492+
WithSince("v1.40.0").
493+
WithPresets(linter.PresetBugs).
494+
WithURL("https://github.com/kyoh86/looppointer"),
491495

492496
// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
493497
linter.NewConfig(golinters.NewNoLintLint()).

test/testdata/looppointer.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//args: -Elooppointer
2+
package testdata
3+
4+
func lintLoopPointer() {
5+
type domain struct {
6+
property *int
7+
}
8+
9+
type dto struct {
10+
prop int
11+
cond bool
12+
}
13+
14+
result := make([]domain, 0, 3)
15+
for _, d := range []dto{
16+
{prop: 10},
17+
{prop: 11},
18+
{prop: 12},
19+
} {
20+
p := domain{}
21+
if !d.cond {
22+
p.property = &d.prop // ERROR "taking a pointer for the loop variable d"
23+
}
24+
result = append(result, p)
25+
}
26+
27+
for _, r := range result {
28+
if r.property != nil {
29+
// expected 10, 11, 12
30+
// obtained 12, 12, 12
31+
println(*r.property)
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)