Skip to content

Commit 1b3cb5c

Browse files
committed
go/analysis/passes/buildtag: revert "suggest fix to remove +build lines"
This CL reverts commit 517957c, CL 694415, because it makes go vet and go test report errors for non-problems in existing code. Bundling the fixer into the vet check was the wrong approach; instead we need a separate fixer, e.g. as part of modernize. Change-Id: I9379571439c6c855668374cc8978cef7a9fbc32b Reviewed-on: https://go-review.googlesource.com/c/tools/+/711320 Reviewed-by: Michael Matloob <matloob@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Matloob <matloob@google.com>
1 parent 1daaaca commit 1b3cb5c

File tree

7 files changed

+22
-67
lines changed

7 files changed

+22
-67
lines changed

go/analysis/passes/buildtag/buildtag.go

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2220
const Doc = "check //go:build and // +build directives"
@@ -57,6 +55,7 @@ func runBuildTag(pass *analysis.Pass) (any, error) {
5755
func 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

10482
func checkOtherFile(pass *analysis.Pass, filename string) error {
@@ -118,15 +96,15 @@ func checkOtherFile(pass *analysis.Pass, filename string) error {
11896
}
11997

12098
type 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

132110
func (check *checker) init(pass *analysis.Pass) {
@@ -294,8 +272,6 @@ func (check *checker) goBuildLine(pos token.Pos, line string) {
294272
}
295273

296274
func (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

360338
func (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
}

go/analysis/passes/buildtag/buildtag_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ func Test(t *testing.T) {
1616
// Because it cares about IgnoredFiles, which most analyzers
1717
// ignore, the test framework will consider expectations in
1818
// ignore files too, but only for this analyzer.
19-
analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), buildtag.Analyzer, "a", "b")
19+
analysistest.Run(t, analysistest.TestData(), buildtag.Analyzer, "a", "b")
2020
}

go/analysis/passes/buildtag/testdata/src/a/buildtag4.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build go1.18 && !(bad || worse)
6-
//want +1 `.build line is no longer needed`
7-
// +build go1.18
8-
//want +1 `.build line is no longer needed`
5+
//go:build !(bad || worse)
96
// +build !bad
10-
//want +1 `.build line is no longer needed`
117
// +build !worse
128

139
package a

go/analysis/passes/buildtag/testdata/src/a/buildtag4.go.golden

Lines changed: 0 additions & 12 deletions
This file was deleted.

go/analysis/passes/buildtag/testdata/src/b/vers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
// want +3 `invalid go version \"go1.20.1\" in build constraint`
56
// want +1 `invalid go version \"go1.20.1\" in build constraint`
67
//go:build go1.20.1
7-
// want +1 `[+]build line is no longer needed` `invalid go version \"go1.20.1\" in build constraint`
88
// +build go1.20.1
99

1010
package b

go/analysis/passes/buildtag/testdata/src/b/vers.go.golden

Lines changed: 0 additions & 9 deletions
This file was deleted.

gopls/internal/test/integration/workspace/standalone_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type I interface {
2929
}
3030
-- lib/ignore.go --
3131
//go:build ignore
32+
// +build ignore
3233
3334
package main
3435
@@ -163,6 +164,7 @@ go 1.18
163164
package lib // without this package, files are loaded as command-line-arguments
164165
-- ignore.go --
165166
//go:build ignore
167+
// +build ignore
166168
167169
package main
168170
@@ -171,6 +173,7 @@ package main
171173
func main() {}
172174
-- standalone.go --
173175
//go:build standalone
176+
// +build standalone
174177
175178
package main
176179

0 commit comments

Comments
 (0)