@@ -15,8 +15,6 @@ import (
1515
1616 "golang.org/x/tools/go/analysis"
1717 "golang.org/x/tools/go/analysis/passes/internal/analysisutil"
18- "golang.org/x/tools/internal/analysisinternal"
19- "golang.org/x/tools/internal/versions"
2018)
2119
2220const Doc = "check //go:build and // +build directives"
@@ -57,6 +55,7 @@ func runBuildTag(pass *analysis.Pass) (any, error) {
5755func checkGoFile (pass * analysis.Pass , f * ast.File ) {
5856 var check checker
5957 check .init (pass )
58+ defer check .finish ()
6059
6160 for _ , group := range f .Comments {
6261 // A +build comment is ignored after or adjoining the package declaration.
@@ -78,27 +77,6 @@ func checkGoFile(pass *analysis.Pass, f *ast.File) {
7877 check .comment (c .Slash , c .Text )
7978 }
8079 }
81-
82- check .finish ()
83-
84- // For Go 1.18+ files, offer a fix to remove the +build lines
85- // if they passed all consistency checks.
86- if check .crossCheck && ! versions .Before (pass .TypesInfo .FileVersions [f ], "go1.18" ) {
87- for _ , rng := range check .plusBuildRanges {
88- check .pass .Report (analysis.Diagnostic {
89- Pos : rng .Pos (),
90- End : rng .End (),
91- Message : "+build line is no longer needed" ,
92- SuggestedFixes : []analysis.SuggestedFix {{
93- Message : "Remove obsolete +build line" ,
94- TextEdits : []analysis.TextEdit {{
95- Pos : rng .Pos (),
96- End : rng .End (),
97- }},
98- }},
99- })
100- }
101- }
10280}
10381
10482func checkOtherFile (pass * analysis.Pass , filename string ) error {
@@ -118,15 +96,15 @@ func checkOtherFile(pass *analysis.Pass, filename string) error {
11896}
11997
12098type checker struct {
121- pass * analysis.Pass
122- plusBuildOK bool // "+build" lines still OK
123- goBuildOK bool // "go:build" lines still OK
124- crossCheck bool // cross-check go:build and +build lines when done reading file
125- inStar bool // currently in a /* */ comment
126- goBuildPos token.Pos // position of first go:build line found
127- plusBuildRanges []analysis. Range // range of each "+build" line found
128- goBuild constraint.Expr // go:build constraint found
129- plusBuild constraint.Expr // AND of +build constraints found
99+ pass * analysis.Pass
100+ plusBuildOK bool // "+build" lines still OK
101+ goBuildOK bool // "go:build" lines still OK
102+ crossCheck bool // cross-check go:build and +build lines when done reading file
103+ inStar bool // currently in a /* */ comment
104+ goBuildPos token.Pos // position of first go:build line found
105+ plusBuildPos token. Pos // position of first "+build" line found
106+ goBuild constraint.Expr // go:build constraint found
107+ plusBuild constraint.Expr // AND of +build constraints found
130108}
131109
132110func (check * checker ) init (pass * analysis.Pass ) {
@@ -294,8 +272,6 @@ func (check *checker) goBuildLine(pos token.Pos, line string) {
294272}
295273
296274func (check * checker ) plusBuildLine (pos token.Pos , line string ) {
297- plusBuildRange := analysisinternal .Range (pos , pos + token .Pos (len (line )))
298-
299275 line = strings .TrimSpace (line )
300276 if ! constraint .IsPlusBuild (line ) {
301277 // Comment with +build but not at beginning.
@@ -310,7 +286,9 @@ func (check *checker) plusBuildLine(pos token.Pos, line string) {
310286 check .crossCheck = false
311287 }
312288
313- check .plusBuildRanges = append (check .plusBuildRanges , plusBuildRange )
289+ if check .plusBuildPos == token .NoPos {
290+ check .plusBuildPos = pos
291+ }
314292
315293 // testing hack: stop at // ERROR
316294 if i := strings .Index (line , " // ERROR " ); i >= 0 {
@@ -358,19 +336,19 @@ func (check *checker) plusBuildLine(pos token.Pos, line string) {
358336}
359337
360338func (check * checker ) finish () {
361- if ! check .crossCheck || len ( check .plusBuildRanges ) == 0 || check .goBuildPos == token .NoPos {
339+ if ! check .crossCheck || check .plusBuildPos == token . NoPos || check .goBuildPos == token .NoPos {
362340 return
363341 }
364342
365343 // Have both //go:build and // +build,
366344 // with no errors found (crossCheck still true).
367345 // Check they match.
346+ var want constraint.Expr
368347 lines , err := constraint .PlusBuildLines (check .goBuild )
369348 if err != nil {
370349 check .pass .Reportf (check .goBuildPos , "%v" , err )
371350 return
372351 }
373- var want constraint.Expr
374352 for _ , line := range lines {
375353 y , err := constraint .Parse (line )
376354 if err != nil {
@@ -385,8 +363,7 @@ func (check *checker) finish() {
385363 }
386364 }
387365 if want .String () != check .plusBuild .String () {
388- check .pass .ReportRangef (check .plusBuildRanges [0 ], "+build lines do not match //go:build condition" )
389- check .crossCheck = false // don't offer fix to remove +build
366+ check .pass .Reportf (check .plusBuildPos , "+build lines do not match //go:build condition" )
390367 return
391368 }
392369}
0 commit comments