-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoioutil.go
57 lines (48 loc) · 1.16 KB
/
noioutil.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
package noioutil
import (
"go/ast"
"go/token"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
const doc = "noioutil finds files using the io/ioutil package."
// Analyzer is the noioutil analyzer.
var Analyzer = &analysis.Analyzer{
Name: "noioutil",
Doc: doc,
Run: run,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
}
func run(pass *analysis.Pass) (interface{}, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodeFilter := []ast.Node{
(*ast.File)(nil),
}
inspect.Preorder(nodeFilter, func(n ast.Node) {
switch n := n.(type) {
case *ast.File:
fileName := n.Name.Name
for _, f := range n.Decls {
switch decl := f.(type) {
case *ast.GenDecl:
if decl.Tok == token.IMPORT {
checkImport(pass, decl, fileName)
}
}
}
}
})
return nil, nil
}
func checkImport(pass *analysis.Pass, decl *ast.GenDecl, fileName string) {
for _, spec := range decl.Specs {
if spec, ok := spec.(*ast.ImportSpec); ok {
if spec.Path.Value == `"io/ioutil"` {
pass.Reportf(spec.Path.Pos(), "\"io/ioutil\" package is used")
}
}
}
}