/
main.go
134 lines (105 loc) · 2.17 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"fmt"
"go/ast"
"go/build"
"go/token"
"os"
"github.com/jessevdk/go-flags"
"github.com/kisielk/gotool"
"golang.org/x/tools/go/loader"
)
func foo() (hey string) {
return
}
func main() {
var opts struct {
Tags []string `long:"tag" description:"Build tags"`
}
pkgs, err := flags.Parse(&opts)
if err != nil {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}
if len(pkgs) == 0 {
pkgs = append(pkgs, ".")
}
if err := lint(pkgs, opts.Tags); err != nil {
fmt.Printf("ERROR: %s\n", err)
os.Exit(1)
}
}
func lint(pkgs []string, tags []string) error {
ctx := build.Default
for _, tag := range tags {
ctx.BuildTags = append(ctx.BuildTags, tag)
}
conf := loader.Config{
Build: &ctx,
}
gotoolCtx := gotool.Context{
BuildContext: ctx,
}
for _, name := range gotoolCtx.ImportPaths(pkgs) {
conf.ImportWithTests(name)
}
prog, err := conf.Load()
if err != nil {
return err
}
foundError := false
for _, pkg := range prog.InitialPackages() {
if pkg.Errors != nil {
fmt.Printf("%s: %#v", pkg.String(), pkg.Errors)
foundError = true
}
}
if foundError {
return fmt.Errorf("Found some initializing errors")
}
for _, pkg := range prog.InitialPackages() {
for _, file := range pkg.Files {
ast.Walk(walkForFunctions{
FileSet: prog.Fset,
}, file)
}
}
return nil
}
type walkForFunctions struct {
FileSet *token.FileSet
}
func (w walkForFunctions) Visit(n ast.Node) ast.Visitor {
if n == nil {
return nil
}
if f, ok := n.(*ast.FuncDecl); ok && hasNamedReturnArgument(f) {
ast.Walk(walkForNakedReturn{
FileSet: w.FileSet,
}, f.Body)
}
return w
}
func hasNamedReturnArgument(f *ast.FuncDecl) bool {
if f.Type.Results == nil {
return false
}
for _, r := range f.Type.Results.List {
if len(r.Names) > 0 && r.Names[0].String() != "" {
return true
}
}
return false
}
type walkForNakedReturn struct {
FileSet *token.FileSet
}
func (w walkForNakedReturn) Visit(n ast.Node) ast.Visitor {
if n == nil {
return nil
}
if r, ok := n.(*ast.ReturnStmt); ok && len(r.Results) == 0 {
fmt.Printf("%s: Naked return in function with named return arguments\n", w.FileSet.Position(r.Pos()))
}
return w
}