blob: b98a92144b94497be8eff420c8e58f84997a93e4 [file] [log] [blame]
Lorenz Bruncb2dcf62021-11-22 22:57:34 +01001// Package noioutil contains a Go analysis pass designed to prevent use of the
2// deprecated ioutil package for which a tree-wide migration was already done.
3package noioutil
4
5import (
Lorenz Bruncb2dcf62021-11-22 22:57:34 +01006 "strconv"
Lorenz Bruncb2dcf62021-11-22 22:57:34 +01007
8 "golang.org/x/tools/go/analysis"
Serge Bazanski8bd2ab12021-12-17 17:32:16 +01009
10 alib "source.monogon.dev/build/analysis/lib"
Lorenz Bruncb2dcf62021-11-22 22:57:34 +010011)
12
13var Analyzer = &analysis.Analyzer{
14 Name: "noioutil",
15 Doc: "noioutil checks for imports of the deprecated ioutil package",
16 Run: run,
17}
18
19func run(p *analysis.Pass) (interface{}, error) {
20 for _, file := range p.Files {
Serge Bazanski8bd2ab12021-12-17 17:32:16 +010021 if alib.IsGeneratedFile(file) {
Lorenz Bruncb2dcf62021-11-22 22:57:34 +010022 continue
23 }
24 for _, i := range file.Imports {
25 importPath, err := strconv.Unquote(i.Path.Value)
26 if err != nil {
27 continue
28 }
29 if importPath == "io/ioutil" {
30 p.Report(analysis.Diagnostic{
31 Pos: i.Path.ValuePos,
32 End: i.Path.End(),
33 Message: "File imports the deprecated io/ioutil package. See https://pkg.go.dev/io/ioutil for replacements.",
34 })
35 }
36 }
37 }
38
39 return nil, nil
40}