| Russ Cox | 2a1b4a8 | 2011-03-28 23:29:00 -0400 | [diff] [blame] | 1 | // 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 | |
| 5 | package main |
| 6 | |
| 7 | import ( |
| 8 | "go/ast" |
| 9 | ) |
| 10 | |
| David Symonds | d26144b | 2011-11-04 08:34:37 +1100 | [diff] [blame] | 11 | func init() { |
| 12 | register(netdialFix) |
| 13 | register(tlsdialFix) |
| 14 | register(netlookupFix) |
| 15 | } |
| 16 | |
| Russ Cox | 2a1b4a8 | 2011-03-28 23:29:00 -0400 | [diff] [blame] | 17 | var netdialFix = fix{ |
| 18 | "netdial", |
| David Symonds | d26144b | 2011-11-04 08:34:37 +1100 | [diff] [blame] | 19 | "2011-03-28", |
| Russ Cox | 2a1b4a8 | 2011-03-28 23:29:00 -0400 | [diff] [blame] | 20 | netdial, |
| Robert Griesemer | 1afc37f | 2011-03-29 18:30:59 -0700 | [diff] [blame] | 21 | `Adapt 3-argument calls of net.Dial to use 2-argument form. |
| Russ Cox | 2a1b4a8 | 2011-03-28 23:29:00 -0400 | [diff] [blame] | 22 | |
| 23 | http://codereview.appspot.com/4244055 |
| 24 | `, |
| 25 | } |
| 26 | |
| 27 | var tlsdialFix = fix{ |
| 28 | "tlsdial", |
| David Symonds | d26144b | 2011-11-04 08:34:37 +1100 | [diff] [blame] | 29 | "2011-03-28", |
| Russ Cox | 2a1b4a8 | 2011-03-28 23:29:00 -0400 | [diff] [blame] | 30 | tlsdial, |
| Robert Griesemer | 1afc37f | 2011-03-29 18:30:59 -0700 | [diff] [blame] | 31 | `Adapt 4-argument calls of tls.Dial to use 3-argument form. |
| Russ Cox | 2a1b4a8 | 2011-03-28 23:29:00 -0400 | [diff] [blame] | 32 | |
| 33 | http://codereview.appspot.com/4244055 |
| 34 | `, |
| 35 | } |
| 36 | |
| 37 | var netlookupFix = fix{ |
| 38 | "netlookup", |
| David Symonds | d26144b | 2011-11-04 08:34:37 +1100 | [diff] [blame] | 39 | "2011-03-28", |
| Russ Cox | 2a1b4a8 | 2011-03-28 23:29:00 -0400 | [diff] [blame] | 40 | netlookup, |
| Robert Griesemer | 1afc37f | 2011-03-29 18:30:59 -0700 | [diff] [blame] | 41 | `Adapt 3-result calls to net.LookupHost to use 2-result form. |
| Russ Cox | 2a1b4a8 | 2011-03-28 23:29:00 -0400 | [diff] [blame] | 42 | |
| 43 | http://codereview.appspot.com/4244055 |
| 44 | `, |
| 45 | } |
| 46 | |
| Russ Cox | 2a1b4a8 | 2011-03-28 23:29:00 -0400 | [diff] [blame] | 47 | func netdial(f *ast.File) bool { |
| 48 | if !imports(f, "net") { |
| 49 | return false |
| 50 | } |
| 51 | |
| 52 | fixed := false |
| Russ Cox | 877c189 | 2011-04-08 12:27:08 -0400 | [diff] [blame] | 53 | walk(f, func(n interface{}) { |
| Russ Cox | 2a1b4a8 | 2011-03-28 23:29:00 -0400 | [diff] [blame] | 54 | 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 | |
| 70 | func tlsdial(f *ast.File) bool { |
| 71 | if !imports(f, "crypto/tls") { |
| 72 | return false |
| 73 | } |
| 74 | |
| 75 | fixed := false |
| Russ Cox | 877c189 | 2011-04-08 12:27:08 -0400 | [diff] [blame] | 76 | walk(f, func(n interface{}) { |
| Russ Cox | 2a1b4a8 | 2011-03-28 23:29:00 -0400 | [diff] [blame] | 77 | 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 | |
| 94 | func netlookup(f *ast.File) bool { |
| 95 | if !imports(f, "net") { |
| 96 | return false |
| 97 | } |
| 98 | |
| 99 | fixed := false |
| Russ Cox | 877c189 | 2011-04-08 12:27:08 -0400 | [diff] [blame] | 100 | walk(f, func(n interface{}) { |
| Russ Cox | 2a1b4a8 | 2011-03-28 23:29:00 -0400 | [diff] [blame] | 101 | 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 | } |