blob: 2de994cffe55b404d69484bfc36e2909f2c85f1f [file] [log] [blame]
Russ Cox2a1b4a82011-03-28 23:29:00 -04001// Copyright 2011 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package main
6
7import (
8 "go/ast"
9)
10
David Symondsd26144b2011-11-04 08:34:37 +110011func init() {
12 register(netdialFix)
13 register(tlsdialFix)
14 register(netlookupFix)
15}
16
Russ Cox2a1b4a82011-03-28 23:29:00 -040017var netdialFix = fix{
18 "netdial",
David Symondsd26144b2011-11-04 08:34:37 +110019 "2011-03-28",
Russ Cox2a1b4a82011-03-28 23:29:00 -040020 netdial,
Robert Griesemer1afc37f2011-03-29 18:30:59 -070021 `Adapt 3-argument calls of net.Dial to use 2-argument form.
Russ Cox2a1b4a82011-03-28 23:29:00 -040022
23http://codereview.appspot.com/4244055
24`,
25}
26
27var tlsdialFix = fix{
28 "tlsdial",
David Symondsd26144b2011-11-04 08:34:37 +110029 "2011-03-28",
Russ Cox2a1b4a82011-03-28 23:29:00 -040030 tlsdial,
Robert Griesemer1afc37f2011-03-29 18:30:59 -070031 `Adapt 4-argument calls of tls.Dial to use 3-argument form.
Russ Cox2a1b4a82011-03-28 23:29:00 -040032
33http://codereview.appspot.com/4244055
34`,
35}
36
37var netlookupFix = fix{
38 "netlookup",
David Symondsd26144b2011-11-04 08:34:37 +110039 "2011-03-28",
Russ Cox2a1b4a82011-03-28 23:29:00 -040040 netlookup,
Robert Griesemer1afc37f2011-03-29 18:30:59 -070041 `Adapt 3-result calls to net.LookupHost to use 2-result form.
Russ Cox2a1b4a82011-03-28 23:29:00 -040042
43http://codereview.appspot.com/4244055
44`,
45}
46
Russ Cox2a1b4a82011-03-28 23:29:00 -040047func netdial(f *ast.File) bool {
48 if !imports(f, "net") {
49 return false
50 }
51
52 fixed := false
Russ Cox877c1892011-04-08 12:27:08 -040053 walk(f, func(n interface{}) {
Russ Cox2a1b4a82011-03-28 23:29:00 -040054 call, ok := n.(*ast.CallExpr)
55 if !ok || !isPkgDot(call.Fun, "net", "Dial") || len(call.Args) != 3 {
56 return
57 }
58 // net.Dial(a, "", b) -> net.Dial(a, b)
59 if !isEmptyString(call.Args[1]) {
60 warn(call.Pos(), "call to net.Dial with non-empty second argument")
61 return
62 }
63 call.Args[1] = call.Args[2]
64 call.Args = call.Args[:2]
65 fixed = true
66 })
67 return fixed
68}
69
70func tlsdial(f *ast.File) bool {
71 if !imports(f, "crypto/tls") {
72 return false
73 }
74
75 fixed := false
Russ Cox877c1892011-04-08 12:27:08 -040076 walk(f, func(n interface{}) {
Russ Cox2a1b4a82011-03-28 23:29:00 -040077 call, ok := n.(*ast.CallExpr)
78 if !ok || !isPkgDot(call.Fun, "tls", "Dial") || len(call.Args) != 4 {
79 return
80 }
81 // tls.Dial(a, "", b, c) -> tls.Dial(a, b, c)
82 if !isEmptyString(call.Args[1]) {
83 warn(call.Pos(), "call to tls.Dial with non-empty second argument")
84 return
85 }
86 call.Args[1] = call.Args[2]
87 call.Args[2] = call.Args[3]
88 call.Args = call.Args[:3]
89 fixed = true
90 })
91 return fixed
92}
93
94func netlookup(f *ast.File) bool {
95 if !imports(f, "net") {
96 return false
97 }
98
99 fixed := false
Russ Cox877c1892011-04-08 12:27:08 -0400100 walk(f, func(n interface{}) {
Russ Cox2a1b4a82011-03-28 23:29:00 -0400101 as, ok := n.(*ast.AssignStmt)
102 if !ok || len(as.Lhs) != 3 || len(as.Rhs) != 1 {
103 return
104 }
105 call, ok := as.Rhs[0].(*ast.CallExpr)
106 if !ok || !isPkgDot(call.Fun, "net", "LookupHost") {
107 return
108 }
109 if !isBlank(as.Lhs[2]) {
110 warn(as.Pos(), "call to net.LookupHost expecting cname; use net.LookupCNAME")
111 return
112 }
113 as.Lhs = as.Lhs[:2]
114 fixed = true
115 })
116 return fixed
117}