From ead11f7a600b4e4fea739e3c58b5419dd787722c Mon Sep 17 00:00:00 2001 From: ivila <390810839@qq.com> Date: Thu, 6 Jan 2022 01:58:18 +0800 Subject: [PATCH] fix: multiple delcaration in same line (#8) Author: Zehui --- makezero/makezero.go | 25 ++++++++++++++++++------ makezero/makezero_test.go | 40 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/makezero/makezero.go b/makezero/makezero.go index db9b45a..89cfcf4 100644 --- a/makezero/makezero.go +++ b/makezero/makezero.go @@ -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 @@ -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 } @@ -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, @@ -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{ @@ -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 } @@ -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 { diff --git a/makezero/makezero_test.go b/makezero/makezero_test.go index b48c178..22e0d18 100644 --- a/makezero/makezero_test.go +++ b/makezero/makezero_test.go @@ -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))