Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1213)

Unified Diff: src/cmd/vet/main.go

Issue 6494075: code review 6494075: vet: add range variable misuse detection (Closed)
Patch Set: diff -r 42b884930d7c https://go.googlecode.com/hg Created 11 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/cmd/vet/Makefile ('k') | src/cmd/vet/rangeloop.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/cmd/vet/main.go
===================================================================
--- a/src/cmd/vet/main.go
+++ b/src/cmd/vet/main.go
@@ -30,6 +30,7 @@
vetPrintf = flag.Bool("printf", false, "check printf-like invocations")
vetStructTags = flag.Bool("structtags", false, "check that struct field tags have canonical format")
vetUntaggedLiteral = flag.Bool("composites", false, "check that composite literals used type-tagged elements")
+ vetRangeLoops = flag.Bool("rangeloops", false, "check that range loop variables are used correctly")
)
// setExit sets the value for os.Exit when it is called, later. It
@@ -60,7 +61,7 @@
flag.Parse()
// If a check is named explicitly, turn off the 'all' flag.
- if *vetMethods || *vetPrintf || *vetStructTags || *vetUntaggedLiteral {
+ if *vetMethods || *vetPrintf || *vetStructTags || *vetUntaggedLiteral || *vetRangeLoops {
*vetAll = false
}
@@ -197,6 +198,8 @@
f.walkMethodDecl(n)
case *ast.InterfaceType:
f.walkInterfaceType(n)
+ case *ast.RangeStmt:
+ f.walkRangeStmt(n)
}
return f
}
@@ -206,6 +209,16 @@
f.checkFmtPrintfCall(call, name)
}
+// walkCallExpr walks a call expression.
+func (f *File) walkCallExpr(call *ast.CallExpr) {
+ switch x := call.Fun.(type) {
+ case *ast.Ident:
+ f.walkCall(call, x.Name)
+ case *ast.SelectorExpr:
+ f.walkCall(call, x.Sel.Name)
+ }
+}
+
// walkCompositeLit walks a composite literal.
func (f *File) walkCompositeLit(c *ast.CompositeLit) {
f.checkUntaggedLiteral(c)
@@ -242,12 +255,7 @@
}
}
-// walkCallExpr walks a call expression.
-func (f *File) walkCallExpr(call *ast.CallExpr) {
- switch x := call.Fun.(type) {
- case *ast.Ident:
- f.walkCall(call, x.Name)
- case *ast.SelectorExpr:
- f.walkCall(call, x.Sel.Name)
- }
+// walkRangeStmt walks a range statment.
+func (f *File) walkRangeStmt(n *ast.RangeStmt) {
+ checkRangeLoop(f, n)
}
« no previous file with comments | « src/cmd/vet/Makefile ('k') | src/cmd/vet/rangeloop.go » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b