summaryrefslogtreecommitdiff
path: root/src/cmd/fix/netudpgroup.go
blob: b54beb0de31e26abfa4525c44982e97b7e0ac1e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2011 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"go/ast"
)

func init() {
	register(netudpgroupFix)
}

var netudpgroupFix = fix{
	"netudpgroup",
	"2011-08-18",
	netudpgroup,
	`Adapt 1-argument calls of net.(*UDPConn).JoinGroup, LeaveGroup to use 2-argument form.

http://codereview.appspot.com/4815074
`,
}

func netudpgroup(f *ast.File) bool {
	if !imports(f, "net") {
		return false
	}

	fixed := false
	for _, d := range f.Decls {
		fd, ok := d.(*ast.FuncDecl)
		if !ok || fd.Body == nil {
			continue
		}
		walk(fd.Body, func(n interface{}) {
			ce, ok := n.(*ast.CallExpr)
			if !ok {
				return
			}
			se, ok := ce.Fun.(*ast.SelectorExpr)
			if !ok || len(ce.Args) != 1 {
				return
			}
			switch se.Sel.String() {
			case "JoinGroup", "LeaveGroup":
				// c.JoinGroup(a) -> c.JoinGroup(nil, a)
				// c.LeaveGroup(a) -> c.LeaveGroup(nil, a)
				arg := ce.Args[0]
				ce.Args = make([]ast.Expr, 2)
				ce.Args[0] = ast.NewIdent("nil")
				ce.Args[1] = arg
				fixed = true
			}
		})
	}
	return fixed
}