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
6 changes: 3 additions & 3 deletions pkg/prealloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ func (v *returnsVisitor) Visit(node ast.Node) ast.Visitor {
v.arrayTypes = append(v.arrayTypes, n.Name.Name)
}
}
case *ast.FuncDecl:
if n.Body != nil {
for _, stmt := range n.Body.List {
case *ast.BlockStmt:
if n.List != nil {
for _, stmt := range n.List {
switch s := stmt.(type) {
// Find non pre-allocated slices
case *ast.DeclStmt:
Expand Down
12 changes: 12 additions & 0 deletions prealloc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ func Test_checkForPreallocations(t *testing.T) {
Pos: 102,
DeclaredSliceName: "t",
},
pkg.Hint{
Pos: 820,
DeclaredSliceName: "m",
},
pkg.Hint{
Pos: 936,
DeclaredSliceName: "n",
},
pkg.Hint{
Pos: 1062,
DeclaredSliceName: "o",
},
}

if len(got) != len(want) {
Expand Down
25 changes: 25 additions & 0 deletions testdata/sample.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,31 @@ func main() {
}

_ = v

{
var m []int
for i := range "Hello" {
// m is a candidate for preallocation
m = append(m, i)
}

if true {
var n []int
for i := range "Hello" {
// n is a candidate for preallocation
n = append(n, i)
}

for {
var o []int
for i := range "Hello" {
// o is a candidate for preallocation
o = append(o, i)
}
break
}
}
}
}

func foo(n int) []int {
Expand Down