gsquire / dll

Go linter for finding defer statements inside for loops

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fails to identify defers in nested BlockStmt

dennisfischer opened this issue · comments

Defers in nested BlockStmt are not detected.

package main

import (
	"fmt"
)

func main() {
	for i := 0; i < 5; i++ {
		defer fmt.Println("Hello, playground")
		{
			defer fmt.Println("Hello, playground") //Not detected
		}
		fmt.Println(i)
	}
}

Bug is in

dll/dll.go

Lines 42 to 50 in adb0f52

switch ty := n.(type) {
case *ast.ForStmt:
for _, stmt := range ty.Body.List {
if d, ok := stmt.(*ast.DeferStmt); ok {
w.reports = append(w.reports,
&report{sourceName: sourceName, lineNumber: source.Line(d.Pos())})
}
}
}

Instead of looking into the BlockStmt of the for loop all child elements have to be checked. I haven't worked much with ast package, but I'd assume that you'd need another walker+visitor here.

Thanks for this, I had totally overlooked those types of statements and expressions initially. I believe there is a better way than trying to account for all nodes that can contain defer statements so I will have to take a further look.