Skip to content

Commit

Permalink
fix: multiple delcaration in same line (#8)
Browse files Browse the repository at this point in the history
Author: Zehui <[email protected]>
  • Loading branch information
ivila authored Jan 5, 2022
1 parent b626158 commit ead11f7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
25 changes: 19 additions & 6 deletions makezero/makezero.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ import (
"regexp"
)

// a decl might include multiple var,
// so var name with decl make final uniq obj
type uniqDecl struct {
varName string
decl interface{}
}

type Issue interface {
Details() string
Position() token.Position
Expand Down Expand Up @@ -58,7 +65,7 @@ type visitor struct {
comments []*ast.CommentGroup // comments to apply during this visit
info *types.Info

nonZeroLengthSliceDecls map[interface{}]struct{}
nonZeroLengthSliceDecls map[uniqDecl]struct{}
fset *token.FileSet
issues []Issue
}
Expand All @@ -81,7 +88,7 @@ func (l Linter) Run(fset *token.FileSet, info *types.Info, nodes ...ast.Node) ([
comments = file.Comments
}
visitor := visitor{
nonZeroLengthSliceDecls: make(map[interface{}]struct{}),
nonZeroLengthSliceDecls: make(map[uniqDecl]struct{}),
initLenMustBeZero: l.initLenMustBeZero,
info: info,
fset: fset,
Expand Down Expand Up @@ -116,9 +123,9 @@ func (v *visitor) Visit(node ast.Node) ast.Visitor {
if len(right.Args) == 2 {
// ignore if not a slice or it has explicit zero length
if !v.isSlice(right.Args[0]) {
break
continue
} else if lit, ok := right.Args[1].(*ast.BasicLit); ok && lit.Kind == token.INT && lit.Value == "0" {
break
continue
}
if v.initLenMustBeZero && !v.hasNoLintOnSameLine(fun) {
v.issues = append(v.issues, MustHaveNonZeroInitLenIssue{
Expand Down Expand Up @@ -148,7 +155,10 @@ func (v *visitor) hasNonZeroInitialLength(ident *ast.Ident) bool {
ident.Name, v.fset.Position(ident.Pos()).String())
return false
}
_, exists := v.nonZeroLengthSliceDecls[ident.Obj.Decl]
_, exists := v.nonZeroLengthSliceDecls[uniqDecl{
varName: ident.Obj.Name,
decl: ident.Obj.Decl,
}]
return exists
}

Expand All @@ -160,7 +170,10 @@ func (v *visitor) recordNonZeroLengthSlices(node ast.Node) {
if ident.Obj == nil {
return
}
v.nonZeroLengthSliceDecls[ident.Obj.Decl] = struct{}{}
v.nonZeroLengthSliceDecls[uniqDecl{
varName: ident.Obj.Name,
decl: ident.Obj.Decl,
}] = struct{}{}
}

func (v *visitor) isSlice(node ast.Node) bool {
Expand Down
40 changes: 40 additions & 0 deletions makezero/makezero_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,46 @@ func foo() {
})
}

func TestMultiDeclare(t *testing.T) {
t.Run("handles multi declares in same line", func(t *testing.T) {
t.Run("with just first obj is non-zero", func(t *testing.T) {
linter := NewLinter(false)
expectIssues(t, linter, `
package bar
func foo() {
a, b := make([]int, 10), make([]int, 0)
a = append(a, 10)
b = append(b, 10)
}`, "append to slice `a` with non-zero initialized length at testing.go:6:9")
})

t.Run("with just second obj is non-zero", func(t *testing.T) {
linter := NewLinter(false)
expectIssues(t, linter, `
package bar
func foo() {
a, b := make([]int, 0), make([]int, 10)
a = append(a, 10)
b = append(b, 10)
}`, "append to slice `b` with non-zero initialized length at testing.go:7:9")
})

t.Run("with all obj non-zero", func(t *testing.T) {
linter := NewLinter(false)
expectIssues(t, linter, `
package bar
func foo() {
a, b := make([]int, 10), make([]int, 10)
a = append(a, 10)
b = append(b, 10)
}`, "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")
})
})
}

func expectIssues(t *testing.T, linter *Linter, contents string, issues ...string) {
actualIssues := parseFile(t, linter, contents)
actualIssueStrs := make([]string, 0, len(actualIssues))
Expand Down

0 comments on commit ead11f7

Please sign in to comment.