Skip to content

Commit a5f55a4

Browse files
committed
cmd/fix: add modernize and inline analyzers
We ran 'go mod vendor' to pull in the newly used packages. Also, add a cmd/go script test that minimally exercises each analyzer, analogous to the cmd/vet test. For #75266 For #75267 For #71859 Change-Id: I334daea048e3d2f614a1788292a3175acf173932 Reviewed-on: https://go-review.googlesource.com/c/go/+/710995 Reviewed-by: Michael Matloob <matloob@golang.org> Auto-Submit: Alan Donovan <adonovan@google.com> TryBot-Bypass: Alan Donovan <adonovan@google.com> Reviewed-by: Michael Matloob <matloob@google.com>
1 parent 80876f4 commit a5f55a4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+14862
-12
lines changed

src/cmd/fix/main.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ package main
2222
import (
2323
"cmd/internal/objabi"
2424
"cmd/internal/telemetry/counter"
25+
"slices"
2526

2627
"golang.org/x/tools/go/analysis"
2728
"golang.org/x/tools/go/analysis/passes/buildtag"
2829
"golang.org/x/tools/go/analysis/passes/hostport"
30+
"golang.org/x/tools/go/analysis/passes/inline"
31+
"golang.org/x/tools/go/analysis/passes/modernize"
2932
"golang.org/x/tools/go/analysis/unitchecker"
3033
)
3134

@@ -38,22 +41,23 @@ func main() {
3841
unitchecker.Main(suite...) // (never returns)
3942
}
4043

41-
// The fix suite analyzers produce fixes that are safe to apply.
42-
// (Diagnostics may not describe actual problems,
43-
// but their fixes must be unambiguously safe to apply.)
44-
var suite = []*analysis.Analyzer{
45-
buildtag.Analyzer,
46-
hostport.Analyzer,
47-
// TODO(adonovan): now the modernize (proposal #75266) and
48-
// inline (proposal #75267) analyzers are published, revendor
49-
// x/tools and add them here.
50-
//
51-
// TODO(adonovan):add any other vet analyzers whose fixes are always safe.
44+
// The fix suite analyzers produce fixes are unambiguously safe to apply,
45+
// even if the diagnostics might not describe actual problems.
46+
var suite = slices.Concat(
47+
[]*analysis.Analyzer{
48+
buildtag.Analyzer,
49+
hostport.Analyzer,
50+
inline.Analyzer,
51+
},
52+
modernize.Suite,
53+
// TODO(adonovan): add any other vet analyzers whose fixes are always safe.
5254
// Candidates to audit: sigchanyzer, printf, assign, unreachable.
55+
// Many of staticcheck's analyzers would make good candidates
56+
// (e.g. rewriting WriteString(fmt.Sprintf()) to Fprintf.)
5357
// Rejected:
5458
// - composites: some types (e.g. PointXY{1,2}) don't want field names.
5559
// - timeformat: flipping MM/DD is a behavior change, but the code
5660
// could potentially be a workaround for another bug.
5761
// - stringintconv: offers two fixes, user input required to choose.
5862
// - fieldalignment: poor signal/noise; fix could be a regression.
59-
}
63+
)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Elementary test of each analyzer in the "go fix" suite.
2+
# This is simply to prove that they are running at all;
3+
# detailed behavior is tested in x/tools.
4+
#
5+
# Each assertion matches the expected diff.
6+
#
7+
# Tip: to see the actual stdout,
8+
# temporarily prefix the go command with "! ".
9+
10+
go fix -diff example.com/x
11+
12+
# buildtag
13+
stdout '-// \+build go1.26'
14+
15+
# hostport
16+
stdout 'net.Dial.*net.JoinHostPort'
17+
18+
# inline
19+
stdout 'var three = 1 \+ 2'
20+
21+
# newexpr (proxy for whole modernize suite)
22+
stdout 'var _ = new\(123\)'
23+
24+
-- go.mod --
25+
module example.com/x
26+
go 1.26
27+
28+
-- x.go --
29+
//go:build go1.26
30+
// +build go1.26
31+
32+
// ↑ buildtag
33+
34+
package x
35+
36+
import (
37+
"fmt"
38+
"net"
39+
)
40+
41+
// hostport
42+
var s string
43+
var _, _ = net.Dial("tcp", fmt.Sprintf("%s:%d", s, 80))
44+
45+
//go:fix inline
46+
func add(x, y int) int { return x + y }
47+
48+
// inline
49+
var three = add(1, 2)
50+
51+
// newexpr
52+
func varOf(x int) *int { return &x }
53+
var _ = varOf(123)

src/cmd/vendor/golang.org/x/tools/go/analysis/passes/inline/doc.go

Lines changed: 109 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)