LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2012 The Go Authors. All rights reserved. | 1 // Copyright 2012 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // This file contains the test for unkeyed struct literals. | 5 // This file contains the test for unkeyed struct literals. |
6 | 6 |
7 package main | 7 package main |
8 | 8 |
9 import ( | 9 import ( |
10 "flag" | 10 "flag" |
11 "go/ast" | 11 "go/ast" |
12 "strings" | 12 "strings" |
13 | 13 |
14 "code.google.com/p/go.tools/cmd/vet/whitelist" | 14 "code.google.com/p/go.tools/cmd/vet/whitelist" |
15 ) | 15 ) |
16 | 16 |
17 var compositeWhiteList = flag.Bool("compositewhitelist", true, "use composite wh
ite list; for testing only") | 17 var compositeWhiteList = flag.Bool("compositewhitelist", true, "use composite wh
ite list; for testing only") |
18 | 18 |
19 func init() { | 19 func init() { |
20 register("composites", | 20 register("composites", |
21 "check that composite literals used field-keyed elements", | 21 "check that composite literals used field-keyed elements", |
| 22 filterUnkeyedLiteral, |
22 checkUnkeyedLiteral, | 23 checkUnkeyedLiteral, |
23 compositeLit) | 24 compositeLit) |
24 } | 25 } |
| 26 |
| 27 func filterUnkeyedLiteral(f *File) bool { return true } |
25 | 28 |
26 // checkUnkeyedLiteral checks if a composite literal is a struct literal with | 29 // checkUnkeyedLiteral checks if a composite literal is a struct literal with |
27 // unkeyed fields. | 30 // unkeyed fields. |
28 func checkUnkeyedLiteral(f *File, node ast.Node) { | 31 func checkUnkeyedLiteral(f *File, node ast.Node) { |
29 c := node.(*ast.CompositeLit) | 32 c := node.(*ast.CompositeLit) |
30 typ := c.Type | 33 typ := c.Type |
31 for { | 34 for { |
32 if typ1, ok := c.Type.(*ast.ParenExpr); ok { | 35 if typ1, ok := c.Type.(*ast.ParenExpr); ok { |
33 typ = typ1 | 36 typ = typ1 |
34 continue | 37 continue |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 } | 119 } |
117 } else { | 120 } else { |
118 // Catch `import "pkgName"` or `import "foo/bar/pkgName"
`. | 121 // Catch `import "pkgName"` or `import "foo/bar/pkgName"
`. |
119 if s == pkgName || strings.HasSuffix(s, "/"+pkgName) { | 122 if s == pkgName || strings.HasSuffix(s, "/"+pkgName) { |
120 return s | 123 return s |
121 } | 124 } |
122 } | 125 } |
123 } | 126 } |
124 return "" | 127 return "" |
125 } | 128 } |
LEFT | RIGHT |