| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| Lorenz Brun | cb2dcf6 | 2021-11-22 22:57:34 +0100 | [diff] [blame] | 4 | // Package noioutil contains a Go analysis pass designed to prevent use of the |
| 5 | // deprecated ioutil package for which a tree-wide migration was already done. |
| 6 | package noioutil |
| 7 | |
| 8 | import ( |
| Lorenz Brun | cb2dcf6 | 2021-11-22 22:57:34 +0100 | [diff] [blame] | 9 | "strconv" |
| Lorenz Brun | cb2dcf6 | 2021-11-22 22:57:34 +0100 | [diff] [blame] | 10 | |
| 11 | "golang.org/x/tools/go/analysis" |
| Serge Bazanski | 8bd2ab1 | 2021-12-17 17:32:16 +0100 | [diff] [blame] | 12 | |
| 13 | alib "source.monogon.dev/build/analysis/lib" |
| Lorenz Brun | cb2dcf6 | 2021-11-22 22:57:34 +0100 | [diff] [blame] | 14 | ) |
| 15 | |
| 16 | var Analyzer = &analysis.Analyzer{ |
| 17 | Name: "noioutil", |
| 18 | Doc: "noioutil checks for imports of the deprecated ioutil package", |
| 19 | Run: run, |
| 20 | } |
| 21 | |
| 22 | func run(p *analysis.Pass) (interface{}, error) { |
| 23 | for _, file := range p.Files { |
| Serge Bazanski | 8bd2ab1 | 2021-12-17 17:32:16 +0100 | [diff] [blame] | 24 | if alib.IsGeneratedFile(file) { |
| Lorenz Brun | cb2dcf6 | 2021-11-22 22:57:34 +0100 | [diff] [blame] | 25 | continue |
| 26 | } |
| 27 | for _, i := range file.Imports { |
| 28 | importPath, err := strconv.Unquote(i.Path.Value) |
| 29 | if err != nil { |
| 30 | continue |
| 31 | } |
| 32 | if importPath == "io/ioutil" { |
| 33 | p.Report(analysis.Diagnostic{ |
| 34 | Pos: i.Path.ValuePos, |
| 35 | End: i.Path.End(), |
| 36 | Message: "File imports the deprecated io/ioutil package. See https://pkg.go.dev/io/ioutil for replacements.", |
| 37 | }) |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return nil, nil |
| 43 | } |