|
68 | 68 |
|
69 | 69 | // UncheckedError indicates the position of an unchecked error return.
|
70 | 70 | type UncheckedError struct {
|
71 |
| - Pos token.Position |
72 |
| - Line string |
73 |
| - FuncName string |
| 71 | + Pos token.Position |
| 72 | + Line string |
| 73 | + FuncName string |
| 74 | + SelectorName string |
74 | 75 | }
|
75 | 76 |
|
76 | 77 | // Result is returned from the CheckPackage function, and holds all the errors
|
@@ -335,6 +336,37 @@ func (v *visitor) fullName(call *ast.CallExpr) string {
|
335 | 336 | return fn.FullName()
|
336 | 337 | }
|
337 | 338 |
|
| 339 | +func getSelectorName(sel *ast.SelectorExpr) string { |
| 340 | + if ident, ok := sel.X.(*ast.Ident); ok { |
| 341 | + return fmt.Sprintf("%s.%s", ident.Name, sel.Sel.Name) |
| 342 | + } |
| 343 | + if s, ok := sel.X.(*ast.SelectorExpr); ok { |
| 344 | + return fmt.Sprintf("%s.%s", getSelectorName(s), sel.Sel.Name) |
| 345 | + } |
| 346 | + |
| 347 | + return "" |
| 348 | +} |
| 349 | + |
| 350 | +// selectorName will return a name for a called function |
| 351 | +// if the function is the result of a selector. Otherwise it will return |
| 352 | +// the empty string. |
| 353 | +// |
| 354 | +// The name is fully qualified by the import path, possible type, |
| 355 | +// function/method name and pointer receiver. |
| 356 | +// |
| 357 | +// For example, |
| 358 | +// - for "fmt.Printf(...)" it will return "fmt.Printf" |
| 359 | +// - for "base64.StdEncoding.Decode(...)" it will return "base64.StdEncoding.Decode" |
| 360 | +// - for "myFunc()" it will return "" |
| 361 | +func (v *visitor) selectorName(call *ast.CallExpr) string { |
| 362 | + sel, _, ok := v.selectorAndFunc(call) |
| 363 | + if !ok { |
| 364 | + return "" |
| 365 | + } |
| 366 | + |
| 367 | + return getSelectorName(sel) |
| 368 | +} |
| 369 | + |
338 | 370 | // namesForExcludeCheck will return a list of fully-qualified function names
|
339 | 371 | // from a function call that can be used to check against the exclusion list.
|
340 | 372 | //
|
@@ -531,11 +563,13 @@ func (v *visitor) addErrorAtPosition(position token.Pos, call *ast.CallExpr) {
|
531 | 563 | }
|
532 | 564 |
|
533 | 565 | var name string
|
| 566 | + var sel string |
534 | 567 | if call != nil {
|
535 | 568 | name = v.fullName(call)
|
| 569 | + sel = v.selectorName(call) |
536 | 570 | }
|
537 | 571 |
|
538 |
| - v.errors = append(v.errors, UncheckedError{pos, line, name}) |
| 572 | + v.errors = append(v.errors, UncheckedError{pos, line, name, sel}) |
539 | 573 | }
|
540 | 574 |
|
541 | 575 | func readfile(filename string) []string {
|
|
0 commit comments