Skip to content

Commit ead11f7

Browse files
authored
fix: multiple delcaration in same line (#8)
Author: Zehui <[email protected]>
1 parent b626158 commit ead11f7

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

makezero/makezero.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ import (
1212
"regexp"
1313
)
1414

15+
// a decl might include multiple var,
16+
// so var name with decl make final uniq obj
17+
type uniqDecl struct {
18+
varName string
19+
decl interface{}
20+
}
21+
1522
type Issue interface {
1623
Details() string
1724
Position() token.Position
@@ -58,7 +65,7 @@ type visitor struct {
5865
comments []*ast.CommentGroup // comments to apply during this visit
5966
info *types.Info
6067

61-
nonZeroLengthSliceDecls map[interface{}]struct{}
68+
nonZeroLengthSliceDecls map[uniqDecl]struct{}
6269
fset *token.FileSet
6370
issues []Issue
6471
}
@@ -81,7 +88,7 @@ func (l Linter) Run(fset *token.FileSet, info *types.Info, nodes ...ast.Node) ([
8188
comments = file.Comments
8289
}
8390
visitor := visitor{
84-
nonZeroLengthSliceDecls: make(map[interface{}]struct{}),
91+
nonZeroLengthSliceDecls: make(map[uniqDecl]struct{}),
8592
initLenMustBeZero: l.initLenMustBeZero,
8693
info: info,
8794
fset: fset,
@@ -116,9 +123,9 @@ func (v *visitor) Visit(node ast.Node) ast.Visitor {
116123
if len(right.Args) == 2 {
117124
// ignore if not a slice or it has explicit zero length
118125
if !v.isSlice(right.Args[0]) {
119-
break
126+
continue
120127
} else if lit, ok := right.Args[1].(*ast.BasicLit); ok && lit.Kind == token.INT && lit.Value == "0" {
121-
break
128+
continue
122129
}
123130
if v.initLenMustBeZero && !v.hasNoLintOnSameLine(fun) {
124131
v.issues = append(v.issues, MustHaveNonZeroInitLenIssue{
@@ -148,7 +155,10 @@ func (v *visitor) hasNonZeroInitialLength(ident *ast.Ident) bool {
148155
ident.Name, v.fset.Position(ident.Pos()).String())
149156
return false
150157
}
151-
_, exists := v.nonZeroLengthSliceDecls[ident.Obj.Decl]
158+
_, exists := v.nonZeroLengthSliceDecls[uniqDecl{
159+
varName: ident.Obj.Name,
160+
decl: ident.Obj.Decl,
161+
}]
152162
return exists
153163
}
154164

@@ -160,7 +170,10 @@ func (v *visitor) recordNonZeroLengthSlices(node ast.Node) {
160170
if ident.Obj == nil {
161171
return
162172
}
163-
v.nonZeroLengthSliceDecls[ident.Obj.Decl] = struct{}{}
173+
v.nonZeroLengthSliceDecls[uniqDecl{
174+
varName: ident.Obj.Name,
175+
decl: ident.Obj.Decl,
176+
}] = struct{}{}
164177
}
165178

166179
func (v *visitor) isSlice(node ast.Node) bool {

makezero/makezero_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,46 @@ func foo() {
7979
})
8080
}
8181

82+
func TestMultiDeclare(t *testing.T) {
83+
t.Run("handles multi declares in same line", func(t *testing.T) {
84+
t.Run("with just first obj is non-zero", func(t *testing.T) {
85+
linter := NewLinter(false)
86+
expectIssues(t, linter, `
87+
package bar
88+
89+
func foo() {
90+
a, b := make([]int, 10), make([]int, 0)
91+
a = append(a, 10)
92+
b = append(b, 10)
93+
}`, "append to slice `a` with non-zero initialized length at testing.go:6:9")
94+
})
95+
96+
t.Run("with just second obj is non-zero", func(t *testing.T) {
97+
linter := NewLinter(false)
98+
expectIssues(t, linter, `
99+
package bar
100+
101+
func foo() {
102+
a, b := make([]int, 0), make([]int, 10)
103+
a = append(a, 10)
104+
b = append(b, 10)
105+
}`, "append to slice `b` with non-zero initialized length at testing.go:7:9")
106+
})
107+
108+
t.Run("with all obj non-zero", func(t *testing.T) {
109+
linter := NewLinter(false)
110+
expectIssues(t, linter, `
111+
package bar
112+
113+
func foo() {
114+
a, b := make([]int, 10), make([]int, 10)
115+
a = append(a, 10)
116+
b = append(b, 10)
117+
}`, "append to slice `a` with non-zero initialized length at testing.go:6:9", "append to slice `b` with non-zero initialized length at testing.go:7:9")
118+
})
119+
})
120+
}
121+
82122
func expectIssues(t *testing.T, linter *Linter, contents string, issues ...string) {
83123
actualIssues := parseFile(t, linter, contents)
84124
actualIssueStrs := make([]string, 0, len(actualIssues))

0 commit comments

Comments
 (0)