1
2
3
4
5 package modernize
6
7 import (
8 "go/ast"
9 "go/parser"
10 "strings"
11
12 "golang.org/x/tools/go/analysis"
13 "golang.org/x/tools/internal/analysis/analyzerutil"
14 "golang.org/x/tools/internal/versions"
15 )
16
17 var PlusBuildAnalyzer = &analysis.Analyzer{
18 Name: "plusbuild",
19 Doc: analyzerutil.MustExtractDoc(doc, "plusbuild"),
20 URL: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/modernize#plusbuild",
21 Run: plusbuild,
22 }
23
24 func plusbuild(pass *analysis.Pass) (any, error) {
25 check := func(f *ast.File) {
26
27
28 if !analyzerutil.FileUsesGoVersion(pass, f, versions.Go1_18) {
29 return
30 }
31
32
33
34
35
36
37
38
39
40
41
42 for _, g := range f.Comments {
43 sawGoBuild := false
44 for _, c := range g.List {
45 if sawGoBuild && strings.HasPrefix(c.Text, "// +build ") {
46 pass.Report(analysis.Diagnostic{
47 Pos: c.Pos(),
48 End: c.End(),
49 Message: "+build line is no longer needed",
50 SuggestedFixes: []analysis.SuggestedFix{{
51 Message: "Remove obsolete +build line",
52 TextEdits: []analysis.TextEdit{{
53 Pos: c.Pos(),
54 End: c.End(),
55 }},
56 }},
57 })
58 break
59 }
60 if strings.HasPrefix(c.Text, "//go:build ") {
61 sawGoBuild = true
62 }
63 }
64 }
65 }
66
67 for _, f := range pass.Files {
68 check(f)
69 }
70 for _, name := range pass.IgnoredFiles {
71 if strings.HasSuffix(name, ".go") {
72 f, err := parser.ParseFile(pass.Fset, name, nil, parser.ParseComments|parser.SkipObjectResolution)
73 if err != nil {
74 continue
75 }
76 check(f)
77 }
78 }
79 return nil, nil
80 }
81
View as plain text