Skip to content

Commit 4653e48

Browse files
idnandrefindleyr
authored andcommitted
gopls/internal/analysis: add skipped analysis simplify on generated code
On generated code, gopls always suggest to simplify code produced literals polluting the "Problems" pane in IDE, while properly refusing to format file on save because it is generated code. This change will make simplifycompositelit, simplifyrange, simplifyslice skipped on generated code, so it will not polluting the "Problems" pane in IDE. Fixes golang/go#67733 Change-Id: I99b3f083ce96594f360576f530cfc7f4b77c1cc1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/598835 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent f855a53 commit 4653e48

File tree

18 files changed

+298
-9
lines changed

18 files changed

+298
-9
lines changed

gopls/doc/analyzers.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,8 @@ will be simplified to:
636636

637637
This is one of the simplifications that "gofmt -s" applies.
638638

639+
This analyzer ignores generated code.
640+
639641
Default: on.
640642

641643
Package documentation: [simplifycompositelit](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/simplifycompositelit)
@@ -662,6 +664,8 @@ will be simplified to:
662664

663665
This is one of the simplifications that "gofmt -s" applies.
664666

667+
This analyzer ignores generated code.
668+
665669
Default: on.
666670

667671
Package documentation: [simplifyrange](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/simplifyrange)
@@ -680,6 +684,8 @@ will be simplified to:
680684

681685
This is one of the simplifications that "gofmt -s" applies.
682686

687+
This analyzer ignores generated code.
688+
683689
Default: on.
684690

685691
Package documentation: [simplifyslice](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/simplifyslice)

gopls/internal/analysis/simplifycompositelit/doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@
1919
// []T{{}, {}}
2020
//
2121
// This is one of the simplifications that "gofmt -s" applies.
22+
//
23+
// This analyzer ignores generated code.
2224
package simplifycompositelit

gopls/internal/analysis/simplifycompositelit/simplifycompositelit.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"golang.org/x/tools/go/analysis"
2020
"golang.org/x/tools/go/analysis/passes/inspect"
2121
"golang.org/x/tools/go/ast/inspector"
22+
"golang.org/x/tools/gopls/internal/util/astutil"
2223
"golang.org/x/tools/internal/analysisinternal"
2324
)
2425

@@ -34,9 +35,21 @@ var Analyzer = &analysis.Analyzer{
3435
}
3536

3637
func run(pass *analysis.Pass) (interface{}, error) {
38+
// Gather information whether file is generated or not
39+
generated := make(map[*token.File]bool)
40+
for _, file := range pass.Files {
41+
if astutil.IsGenerated(file) {
42+
generated[pass.Fset.File(file.Pos())] = true
43+
}
44+
}
45+
3746
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
3847
nodeFilter := []ast.Node{(*ast.CompositeLit)(nil)}
3948
inspect.Preorder(nodeFilter, func(n ast.Node) {
49+
if _, ok := generated[pass.Fset.File(n.Pos())]; ok {
50+
return // skip checking if it's generated code
51+
}
52+
4053
expr := n.(*ast.CompositeLit)
4154

4255
outer := expr

gopls/internal/analysis/simplifycompositelit/simplifycompositelit_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ import (
1313

1414
func Test(t *testing.T) {
1515
testdata := analysistest.TestData()
16-
analysistest.RunWithSuggestedFixes(t, testdata, simplifycompositelit.Analyzer, "a")
16+
analysistest.RunWithSuggestedFixes(t, testdata, simplifycompositelit.Analyzer, "a", "generatedcode")
1717
}

gopls/internal/analysis/simplifycompositelit/testdata/src/generatedcode/generatedcode.go

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Code generated with somegen DO NOT EDIT.
6+
7+
package testdata
8+
9+
type T struct {
10+
x, y int
11+
}
12+
13+
var _ = [42]T{
14+
T{}, // No simplification fix is offered in generated code.
15+
T{1, 2}, // No simplification fix is offered in generated code.
16+
T{3, 4}, // No simplification fix is offered in generated code.
17+
}

gopls/internal/analysis/simplifyrange/doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@
2727
// for range v {...}
2828
//
2929
// This is one of the simplifications that "gofmt -s" applies.
30+
//
31+
// This analyzer ignores generated code.
3032
package simplifyrange

gopls/internal/analysis/simplifyrange/simplifyrange.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"golang.org/x/tools/go/analysis"
1515
"golang.org/x/tools/go/analysis/passes/inspect"
1616
"golang.org/x/tools/go/ast/inspector"
17+
"golang.org/x/tools/gopls/internal/util/astutil"
1718
"golang.org/x/tools/internal/analysisinternal"
1819
)
1920

@@ -29,11 +30,23 @@ var Analyzer = &analysis.Analyzer{
2930
}
3031

3132
func run(pass *analysis.Pass) (interface{}, error) {
33+
// Gather information whether file is generated or not
34+
generated := make(map[*token.File]bool)
35+
for _, file := range pass.Files {
36+
if astutil.IsGenerated(file) {
37+
generated[pass.Fset.File(file.Pos())] = true
38+
}
39+
}
40+
3241
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
3342
nodeFilter := []ast.Node{
3443
(*ast.RangeStmt)(nil),
3544
}
3645
inspect.Preorder(nodeFilter, func(n ast.Node) {
46+
if _, ok := generated[pass.Fset.File(n.Pos())]; ok {
47+
return // skip checking if it's generated code
48+
}
49+
3750
var copy *ast.RangeStmt // shallow-copy the AST before modifying
3851
{
3952
x := *n.(*ast.RangeStmt)

gopls/internal/analysis/simplifyrange/simplifyrange_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
func Test(t *testing.T) {
1717
testdata := analysistest.TestData()
18-
analysistest.RunWithSuggestedFixes(t, testdata, simplifyrange.Analyzer, "a")
18+
analysistest.RunWithSuggestedFixes(t, testdata, simplifyrange.Analyzer, "a", "generatedcode")
1919
if slices.Contains(build.Default.ReleaseTags, "go1.23") { // uses iter.Seq
2020
analysistest.RunWithSuggestedFixes(t, testdata, simplifyrange.Analyzer, "rangeoverfunc")
2121
}

gopls/internal/analysis/simplifyrange/testdata/src/generatedcode/generatedcode.go

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Code generated with somegen DO NOT EDIT.
6+
7+
package testdata
8+
9+
import "log"
10+
11+
func mgeneratedcode() {
12+
maps := make(map[string]string)
13+
for k, _ := range maps { // No simplification fix is offered in generated code.
14+
log.Println(k)
15+
}
16+
for _ = range maps { // No simplification fix is offered in generated code.
17+
}
18+
}

gopls/internal/analysis/simplifyslice/doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@
1919
// s[a:]
2020
//
2121
// This is one of the simplifications that "gofmt -s" applies.
22+
//
23+
// This analyzer ignores generated code.
2224
package simplifyslice

gopls/internal/analysis/simplifyslice/simplifyslice.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import (
1010
"fmt"
1111
"go/ast"
1212
"go/printer"
13+
"go/token"
1314

1415
"golang.org/x/tools/go/analysis"
1516
"golang.org/x/tools/go/analysis/passes/inspect"
1617
"golang.org/x/tools/go/ast/inspector"
18+
"golang.org/x/tools/gopls/internal/util/astutil"
1719
"golang.org/x/tools/internal/analysisinternal"
1820
)
1921

@@ -37,11 +39,23 @@ var Analyzer = &analysis.Analyzer{
3739
// x, y := b[:n], b[n:]
3840

3941
func run(pass *analysis.Pass) (interface{}, error) {
42+
// Gather information whether file is generated or not
43+
generated := make(map[*token.File]bool)
44+
for _, file := range pass.Files {
45+
if astutil.IsGenerated(file) {
46+
generated[pass.Fset.File(file.Pos())] = true
47+
}
48+
}
49+
4050
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
4151
nodeFilter := []ast.Node{
4252
(*ast.SliceExpr)(nil),
4353
}
4454
inspect.Preorder(nodeFilter, func(n ast.Node) {
55+
if _, ok := generated[pass.Fset.File(n.Pos())]; ok {
56+
return // skip checking if it's generated code
57+
}
58+
4559
expr := n.(*ast.SliceExpr)
4660
// - 3-index slices always require the 2nd and 3rd index
4761
if expr.Max != nil {

gopls/internal/analysis/simplifyslice/simplifyslice_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ import (
1313

1414
func Test(t *testing.T) {
1515
testdata := analysistest.TestData()
16-
analysistest.RunWithSuggestedFixes(t, testdata, simplifyslice.Analyzer, "a", "typeparams")
16+
analysistest.RunWithSuggestedFixes(t, testdata, simplifyslice.Analyzer, "a", "generatedcode", "typeparams")
1717
}

gopls/internal/analysis/simplifyslice/testdata/src/generatedcode/generatedcode.go

Lines changed: 72 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2024 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Code generated with somegen DO NOT EDIT.
6+
7+
package testdata
8+
9+
var (
10+
a [10]byte
11+
b [20]float32
12+
s []int
13+
t struct {
14+
s []byte
15+
}
16+
17+
_ = a[0:]
18+
_ = a[1:10]
19+
_ = a[2:len(a)] // No simplification fix is offered in generated code.
20+
_ = a[3:(len(a))]
21+
_ = a[len(a)-1 : len(a)] // No simplification fix is offered in generated code.
22+
_ = a[2:len(a):len(a)]
23+
24+
_ = a[:]
25+
_ = a[:10]
26+
_ = a[:len(a)] // No simplification fix is offered in generated code.
27+
_ = a[:(len(a))]
28+
_ = a[:len(a)-1]
29+
_ = a[:len(a):len(a)]
30+
31+
_ = s[0:]
32+
_ = s[1:10]
33+
_ = s[2:len(s)] // No simplification fix is offered in generated code.
34+
_ = s[3:(len(s))]
35+
_ = s[len(a) : len(s)-1]
36+
_ = s[0:len(b)]
37+
_ = s[2:len(s):len(s)]
38+
39+
_ = s[:]
40+
_ = s[:10]
41+
_ = s[:len(s)] // No simplification fix is offered in generated code.
42+
_ = s[:(len(s))]
43+
_ = s[:len(s)-1]
44+
_ = s[:len(b)]
45+
_ = s[:len(s):len(s)]
46+
47+
_ = t.s[0:]
48+
_ = t.s[1:10]
49+
_ = t.s[2:len(t.s)]
50+
_ = t.s[3:(len(t.s))]
51+
_ = t.s[len(a) : len(t.s)-1]
52+
_ = t.s[0:len(b)]
53+
_ = t.s[2:len(t.s):len(t.s)]
54+
55+
_ = t.s[:]
56+
_ = t.s[:10]
57+
_ = t.s[:len(t.s)]
58+
_ = t.s[:(len(t.s))]
59+
_ = t.s[:len(t.s)-1]
60+
_ = t.s[:len(b)]
61+
_ = t.s[:len(t.s):len(t.s)]
62+
)
63+
64+
func _() {
65+
s := s[0:len(s)] // No simplification fix is offered in generated code.
66+
_ = s
67+
}
68+
69+
func m() {
70+
maps := []int{}
71+
_ = maps[1:len(maps)] // No simplification fix is offered in generated code.
72+
}

0 commit comments

Comments
 (0)