Lorenz Brun | cb2dcf6 | 2021-11-22 22:57:34 +0100 | [diff] [blame] | 1 | // 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. |
| 3 | package noioutil |
| 4 | |
| 5 | import ( |
Lorenz Brun | cb2dcf6 | 2021-11-22 22:57:34 +0100 | [diff] [blame] | 6 | "strconv" |
Lorenz Brun | cb2dcf6 | 2021-11-22 22:57:34 +0100 | [diff] [blame] | 7 | |
| 8 | "golang.org/x/tools/go/analysis" |
Serge Bazanski | 8bd2ab1 | 2021-12-17 17:32:16 +0100 | [diff] [blame] | 9 | |
| 10 | alib "source.monogon.dev/build/analysis/lib" |
Lorenz Brun | cb2dcf6 | 2021-11-22 22:57:34 +0100 | [diff] [blame] | 11 | ) |
| 12 | |
| 13 | var Analyzer = &analysis.Analyzer{ |
| 14 | Name: "noioutil", |
| 15 | Doc: "noioutil checks for imports of the deprecated ioutil package", |
| 16 | Run: run, |
| 17 | } |
| 18 | |
| 19 | func run(p *analysis.Pass) (interface{}, error) { |
| 20 | for _, file := range p.Files { |
Serge Bazanski | 8bd2ab1 | 2021-12-17 17:32:16 +0100 | [diff] [blame] | 21 | if alib.IsGeneratedFile(file) { |
Lorenz Brun | cb2dcf6 | 2021-11-22 22:57:34 +0100 | [diff] [blame] | 22 | 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 | } |