Created
May 25, 2017 11:42
-
-
Save mattn/6bf37bf7260eee9425e9e4f53e2003ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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