Skip to content

Instantly share code, notes, and snippets.

@mattn
Created May 25, 2017 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattn/6bf37bf7260eee9425e9e4f53e2003ad to your computer and use it in GitHub Desktop.
Save mattn/6bf37bf7260eee9425e9e4f53e2003ad to your computer and use it in GitHub Desktop.
diff --git a/lint.go b/lint.go
index a13129c..6320b24 100644
--- a/lint.go
+++ b/lint.go
@@ -188,6 +188,7 @@ func (f *file) lint() {
f.lintTimeNames()
f.lintContextKeyTypes()
f.lintContextArgs()
+ f.lintDeferInForLoop()
}
type link string
@@ -1598,3 +1599,23 @@ func srcLine(src []byte, p token.Position) string {
}
return string(src[lo:hi])
}
+
+// lintDeferInForLoop examines using defer in for loop.
+func (f *file) lintDeferInForLoop() {
+ var infor bool
+
+ f.walk(func(n ast.Node) bool {
+ switch v := n.(type) {
+ case *ast.FuncDecl, *ast.FuncLit:
+ infor = false
+ return true
+ case *ast.ForStmt:
+ infor = true
+ return true
+ case *ast.DeferStmt:
+ f.errorf(v, 1, category("defer"), "should not use defer in for loop")
+ return true
+ }
+ return true
+ })
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment