Commit e2da3b64 authored by Robert Griesemer's avatar Robert Griesemer

gofmt: simplify "x, _ = range y" to "x = range y"

(inspired by CL 3529041 by hitchmanr@gmail.com)

R=rsc
CC=golang-dev
https://golang.org/cl/3527042
parent 2bdb2e78
......@@ -10,11 +10,13 @@ import (
)
type compositeLitFinder struct{}
type simplifier struct{}
func (f *compositeLitFinder) Visit(node interface{}) ast.Visitor {
if outer, ok := node.(*ast.CompositeLit); ok {
func (s *simplifier) Visit(node interface{}) ast.Visitor {
switch n := node.(type) {
case *ast.CompositeLit:
// array, slice, and map composite literals may be simplified
outer := n
var eltType ast.Expr
switch typ := outer.Type.(type) {
case *ast.ArrayType:
......@@ -41,17 +43,25 @@ func (f *compositeLitFinder) Visit(node interface{}) ast.Visitor {
}
}
// node was simplified - stop walk
// node was simplified - stop walk (there are no subnodes to simplify)
return nil
}
case *ast.RangeStmt:
// range of the form: for x, _ = range v {...}
// can be simplified to: for x = range v {...}
if n.Value != nil {
if ident, ok := n.Value.(*ast.Ident); ok && ident.Name == "_" {
n.Value = nil
}
}
}
// not a composite literal or not simplified - continue walk
return f
return s
}
func simplify(node interface{}) {
var f compositeLitFinder
ast.Walk(&f, node)
var s simplifier
ast.Walk(&s, node)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment