-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathineffassign.go
127 lines (114 loc) · 2.86 KB
/
ineffassign.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
package main
import (
"flag"
"fmt"
"go/ast"
"go/parser"
"go/scanner"
"go/token"
"log"
"os"
"runtime/debug"
"strings"
"sync"
"github.com/gordonklaus/ineffassign/pkg/ineffassign"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/packages"
)
func main() {
log.SetFlags(0)
log.SetPrefix(ineffassign.Analyzer.Name + ": ")
var (
printVersion = flag.Bool("version", false, "print ineffassign version")
_ = flag.Bool("n", false, "no effect (deprecated)")
)
flag.Usage = func() {
paras := strings.Split(ineffassign.Analyzer.Doc, "\n\n")
fmt.Fprintf(os.Stderr, "%s: %s\n\n", ineffassign.Analyzer.Name, paras[0])
fmt.Fprintf(os.Stderr, "Usage: %s [-flag] [package]\n\n", ineffassign.Analyzer.Name)
if len(paras) > 1 {
fmt.Fprintln(os.Stderr, strings.Join(paras[1:], "\n\n"))
}
fmt.Fprintln(os.Stderr, "\nFlags:")
flag.PrintDefaults()
}
flag.Parse()
if *printVersion {
bi, _ := debug.ReadBuildInfo()
log.Print("version ", bi.Main.Version)
return
}
patterns := flag.Args()
if len(patterns) == 0 {
flag.Usage()
os.Exit(1)
}
pkgs, err := load(patterns...)
if err != nil {
log.Print(err)
os.Exit(1)
}
diagnostics := make([][]analysis.Diagnostic, len(pkgs))
var wg sync.WaitGroup
for i, pkg := range pkgs {
i, pkg := i, pkg
wg.Add(1)
go func() {
ineffassign.Analyzer.Run(&analysis.Pass{
Files: pkg.Syntax,
Report: func(d analysis.Diagnostic) {
diagnostics[i] = append(diagnostics[i], d)
},
})
wg.Done()
}()
}
wg.Wait()
exitcode := 0
for i, pkg := range pkgs {
for _, d := range diagnostics[i] {
pos := pkg.Fset.Position(d.Pos)
fmt.Fprintf(os.Stderr, "%s: %s\n", pos, d.Message)
exitcode = 3
}
}
os.Exit(exitcode)
}
func load(patterns ...string) ([]*packages.Package, error) {
conf := &packages.Config{
// We would use NeedSyntax, but then Package.Fset is nil (https://github.com/golang/go/issues/48226).
Mode: packages.NeedFiles,
Tests: true,
}
pkgs, err := packages.Load(conf, patterns...)
if err != nil {
return nil, err
}
for _, pkg := range pkgs {
pkg.Fset = token.NewFileSet()
pkg.Syntax = make([]*ast.File, len(pkg.GoFiles))
for i, f := range pkg.GoFiles {
var err error
pkg.Syntax[i], err = parser.ParseFile(pkg.Fset, f, nil, parser.AllErrors|parser.ParseComments)
if errlist, ok := err.(scanner.ErrorList); ok {
for _, err := range errlist {
pkg.Errors = append(pkg.Errors, packages.Error{
Pos: err.Pos.String(),
Msg: err.Msg,
Kind: packages.ParseError,
})
}
} else if err != nil {
return nil, err
}
}
}
if n := packages.PrintErrors(pkgs); n > 1 {
return nil, fmt.Errorf("%d errors during loading", n)
} else if n == 1 {
return nil, fmt.Errorf("error during loading")
} else if len(pkgs) == 0 {
return nil, fmt.Errorf("%s matched no packages", strings.Join(patterns, " "))
}
return pkgs, nil
}