Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions makezero/makezero.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,7 @@ func (v *visitor) isSlice(node ast.Node) bool {
obj := ident.Obj
if obj == nil {
if v.info != nil {
_, ok := v.info.ObjectOf(ident).Type().(*types.Slice)
return ok
return v.isSliceType(v.info.ObjectOf(ident).Type())
}
return false
}
Expand All @@ -211,12 +210,28 @@ func (v *visitor) isSlice(node ast.Node) bool {
node = spec.Type
}

// handle imported types (e.g., types.S)
if sel, ok := node.(*ast.SelectorExpr); ok {
if v.info != nil {
return v.isSliceType(v.info.ObjectOf(sel.Sel).Type())
}
return false
}

if node, ok := node.(*ast.ArrayType); ok {
return node.Len == nil // only slices have zero length
}
return false
}

func (v *visitor) isSliceType(t types.Type) bool {
if t == nil {
return false
}
_, ok := t.Underlying().(*types.Slice)
return ok
}

func (v *visitor) hasNoLintOnSameLine(node ast.Node) bool {
nolint := regexp.MustCompile(`^\s*nozero\b`)
nodePos := v.fset.Position(node.Pos())
Expand Down
4 changes: 2 additions & 2 deletions pkg/analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func TestAppend(t *testing.T) {
testdata := analysistest.TestData()
a := analyzer.NewAnalyzer()
analysistest.Run(t, testdata, a, "./append")
analysistest.Run(t, testdata, a, "./src/append")
}

func TestAlways(t *testing.T) {
Expand All @@ -20,5 +20,5 @@ func TestAlways(t *testing.T) {
if err != nil {
t.Fatalf("expected no error but got %q", err)
}
analysistest.Run(t, testdata, a, "./always")
analysistest.Run(t, testdata, a, "./src/always")
}
8 changes: 0 additions & 8 deletions pkg/analyzer/testdata/always/append.go

This file was deleted.

9 changes: 0 additions & 9 deletions pkg/analyzer/testdata/append/append.go

This file was deleted.

22 changes: 22 additions & 0 deletions pkg/analyzer/testdata/src/always/append.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package always

import (
"fmt"
"typespkg"
)

func Foo() {
x := make([]int, 5) // want "slice `x` does not have non-zero initial length"
fmt.Println(x)
}

func Bar() {
x := make(typespkg.S, 10) // want "slice `x` does not have non-zero initial length"
x = append(x, "...") // want "append to slice `x` with non-zero initialized length"
fmt.Println(x)
}

func Baz() {
x := make(typespkg.M, 10)
fmt.Println(x)
}
23 changes: 23 additions & 0 deletions pkg/analyzer/testdata/src/append/append.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package append

import (
"fmt"
"typespkg"
)

func Foo() {
x := make([]int, 5)
x = append(x, 1) // want "append to slice `x` with non-zero initialized length"
fmt.Println(x)
}

func Bar() {
x := make(typespkg.S, 10)
x = append(x, "...") // want "append to slice `x` with non-zero initialized length"
fmt.Println(x)
}

func Baz() {
x := make(typespkg.M, 10)
fmt.Println(x)
}
6 changes: 6 additions & 0 deletions pkg/analyzer/testdata/src/typespkg/typespkg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package typespkg

type (
S []string
M map[string]int
)