LEFT | RIGHT |
1 // Copyright 2013 The Go Authors. All rights reserved. | 1 // Copyright 2013 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 package types | 5 package types |
6 | 6 |
7 import ( | 7 import ( |
8 "errors" | 8 "errors" |
9 "fmt" | 9 "fmt" |
10 "go/ast" | 10 "go/ast" |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 if ident.Name == "init" { | 106 if ident.Name == "init" { |
107 check.errorf(ident.Pos(), "cannot declare init - must be func") | 107 check.errorf(ident.Pos(), "cannot declare init - must be func") |
108 return | 108 return |
109 } | 109 } |
110 | 110 |
111 check.declare(check.pkg.scope, ident, obj) | 111 check.declare(check.pkg.scope, ident, obj) |
112 check.objMap[obj] = d | 112 check.objMap[obj] = d |
113 obj.setOrder(uint32(len(check.objMap))) | 113 obj.setOrder(uint32(len(check.objMap))) |
114 } | 114 } |
115 | 115 |
116 // filename returns a file name suitable for debugging for the given file. | 116 // filename returns a filename suitable for debugging output. |
117 func (check *Checker) filename(fileNo int) string { | 117 func (check *Checker) filename(fileNo int) string { |
118 file := check.files[fileNo] | 118 file := check.files[fileNo] |
119 if pos := file.Pos(); pos.IsValid() { | 119 if pos := file.Pos(); pos.IsValid() { |
120 return check.fset.File(pos).Name() | 120 return check.fset.File(pos).Name() |
121 } | 121 } |
122 return fmt.Sprintf("file[%d]", fileNo) | 122 return fmt.Sprintf("file[%d]", fileNo) |
123 } | 123 } |
124 | 124 |
125 // collectObjects collects all file and package objects and inserts them | 125 // collectObjects collects all file and package objects and inserts them |
126 // into their respective scopes. It also performs imports and associates | 126 // into their respective scopes. It also performs imports and associates |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 // (We m
ust not modify their existing position | 227 // (We m
ust not modify their existing position |
228 // infor
mation because the same package - found | 228 // infor
mation because the same package - found |
229 // via C
onfig.Packages - may be dot-imported in | 229 // via C
onfig.Packages - may be dot-imported in |
230 // anoth
er package!) | 230 // anoth
er package!) |
231 check.de
clare(fileScope, nil, obj) | 231 check.de
clare(fileScope, nil, obj) |
232 check.re
cordImplicit(s, obj) | 232 check.re
cordImplicit(s, obj) |
233 } | 233 } |
234 } | 234 } |
235 // add position to set o
f dot-import positions for this file | 235 // add position to set o
f dot-import positions for this file |
236 // (this is only needed
for "imported but not used" errors) | 236 // (this is only needed
for "imported but not used" errors) |
237 » » » » » » » check.addUnusedDotImport
(fileNo, imp, s.Pos()) | 237 » » » » » » » check.addUnusedDotImport
(fileScope, imp, s.Pos()) |
238 } else { | 238 } else { |
239 // declare imported pack
age object in file scope | 239 // declare imported pack
age object in file scope |
240 check.declare(fileScope,
nil, obj) | 240 check.declare(fileScope,
nil, obj) |
241 } | 241 } |
242 | 242 |
243 case *ast.ValueSpec: | 243 case *ast.ValueSpec: |
244 switch d.Tok { | 244 switch d.Tok { |
245 case token.CONST: | 245 case token.CONST: |
246 // determine which initi
alization expressions to use | 246 // determine which initi
alization expressions to use |
247 switch { | 247 switch { |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 // unusedImports checks for unused imports. | 406 // unusedImports checks for unused imports. |
407 func (check *Checker) unusedImports() { | 407 func (check *Checker) unusedImports() { |
408 // if function bodies are not checked, packages' uses are likely missing
- don't check | 408 // if function bodies are not checked, packages' uses are likely missing
- don't check |
409 if check.conf.IgnoreFuncBodies { | 409 if check.conf.IgnoreFuncBodies { |
410 return | 410 return |
411 } | 411 } |
412 | 412 |
413 // spec: "It is illegal (...) to directly import a package without refer
ring to | 413 // spec: "It is illegal (...) to directly import a package without refer
ring to |
414 // any of its exported identifiers. To import a package solely for its s
ide-effects | 414 // any of its exported identifiers. To import a package solely for its s
ide-effects |
415 // (initialization), use the blank identifier as explicit package name." | 415 // (initialization), use the blank identifier as explicit package name." |
| 416 |
| 417 // check use of regular imported packages |
416 for _, scope := range check.pkg.scope.children /* file scopes */ { | 418 for _, scope := range check.pkg.scope.children /* file scopes */ { |
417 for _, obj := range scope.elems { | 419 for _, obj := range scope.elems { |
418 if obj, ok := obj.(*PkgName); ok { | 420 if obj, ok := obj.(*PkgName); ok { |
419 // Unused "blank imports" are automatically igno
red | 421 // Unused "blank imports" are automatically igno
red |
420 // since _ identifiers are not entered into scop
es. | 422 // since _ identifiers are not entered into scop
es. |
421 if !obj.used { | 423 if !obj.used { |
422 path := obj.imported.path | 424 path := obj.imported.path |
423 base := pathLib.Base(path) | 425 base := pathLib.Base(path) |
424 if obj.name == base { | 426 if obj.name == base { |
425 check.softErrorf(obj.pos, "%q im
ported but not used", path) | 427 check.softErrorf(obj.pos, "%q im
ported but not used", path) |
426 } else { | 428 } else { |
427 check.softErrorf(obj.pos, "%q im
ported but not used as %s", path, obj.name) | 429 check.softErrorf(obj.pos, "%q im
ported but not used as %s", path, obj.name) |
428 } | 430 } |
429 } | 431 } |
430 } | 432 } |
431 } | 433 } |
432 } | 434 } |
433 | 435 |
| 436 // check use of dot-imported packages |
434 for _, unusedDotImports := range check.unusedDotImports { | 437 for _, unusedDotImports := range check.unusedDotImports { |
435 for pkg, pos := range unusedDotImports { | 438 for pkg, pos := range unusedDotImports { |
436 check.softErrorf(pos, "%q imported but not used", pkg.pa
th) | 439 check.softErrorf(pos, "%q imported but not used", pkg.pa
th) |
437 } | 440 } |
438 } | 441 } |
439 } | 442 } |
LEFT | RIGHT |