Skip to content

Commit 66c560d

Browse files
xieyuschengopherbot
authored andcommitted
x/tools: apply modernize fixes
The changes are made by running 'modernize -fix ./...' under x/tools. Change-Id: Iefe9fc799edf105b347dcef9a495ed8b12e8e6c6 Reviewed-on: https://go-review.googlesource.com/c/tools/+/662196 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 300a853 commit 66c560d

Some content is hidden

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

85 files changed

+203
-309
lines changed

cmd/callgraph/main.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,7 @@ func init() {
148148
// If $GOMAXPROCS isn't set, use the full capacity of the machine.
149149
// For small machines, use at least 4 threads.
150150
if os.Getenv("GOMAXPROCS") == "" {
151-
n := runtime.NumCPU()
152-
if n < 4 {
153-
n = 4
154-
}
151+
n := max(runtime.NumCPU(), 4)
155152
runtime.GOMAXPROCS(n)
156153
}
157154
}

cmd/deadcode/deadcode_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ func Test(t *testing.T) {
3434
t.Fatal(err)
3535
}
3636
for _, filename := range matches {
37-
filename := filename
3837
t.Run(filename, func(t *testing.T) {
3938
t.Parallel()
4039

cmd/file2fuzz/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434
var encVersion1 = "go test fuzz v1"
3535

3636
func encodeByteSlice(b []byte) []byte {
37-
return []byte(fmt.Sprintf("%s\n[]byte(%q)", encVersion1, b))
37+
return fmt.Appendf(nil, "%s\n[]byte(%q)", encVersion1, b)
3838
}
3939

4040
func usage() {

cmd/godex/writetype.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ package main
1414

1515
import (
1616
"go/types"
17+
"slices"
1718
)
1819

1920
func (p *printer) writeType(this *types.Package, typ types.Type) {
@@ -28,11 +29,9 @@ func (p *printer) writeTypeInternal(this *types.Package, typ types.Type, visited
2829
// practice deeply nested composite types with unnamed component
2930
// types are uncommon. This code is likely more efficient than
3031
// using a map.
31-
for _, t := range visited {
32-
if t == typ {
33-
p.printf("○%T", typ) // cycle to typ
34-
return
35-
}
32+
if slices.Contains(visited, typ) {
33+
p.printf("○%T", typ) // cycle to typ
34+
return
3635
}
3736
visited = append(visited, typ)
3837

@@ -72,7 +71,7 @@ func (p *printer) writeTypeInternal(this *types.Package, typ types.Type, visited
7271

7372
p.print("struct {\n")
7473
p.indent++
75-
for i := 0; i < n; i++ {
74+
for i := range n {
7675
f := t.Field(i)
7776
if !f.Anonymous() {
7877
p.printf("%s ", f.Name())
@@ -120,7 +119,7 @@ func (p *printer) writeTypeInternal(this *types.Package, typ types.Type, visited
120119
if GcCompatibilityMode {
121120
// print flattened interface
122121
// (useful to compare against gc-generated interfaces)
123-
for i := 0; i < n; i++ {
122+
for i := range n {
124123
m := t.Method(i)
125124
p.print(m.Name())
126125
p.writeSignatureInternal(this, m.Type().(*types.Signature), visited)

cmd/godoc/godoc_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"os/exec"
1717
"regexp"
1818
"runtime"
19+
"slices"
1920
"strings"
2021
"sync"
2122
"testing"
@@ -127,12 +128,7 @@ func waitForServer(t *testing.T, ctx context.Context, url, match string, reverse
127128
// hasTag checks whether a given release tag is contained in the current version
128129
// of the go binary.
129130
func hasTag(t string) bool {
130-
for _, v := range build.Default.ReleaseTags {
131-
if t == v {
132-
return true
133-
}
134-
}
135-
return false
131+
return slices.Contains(build.Default.ReleaseTags, t)
136132
}
137133

138134
func TestURL(t *testing.T) {

cmd/godoc/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func loggingHandler(h http.Handler) http.Handler {
114114
func handleURLFlag() {
115115
// Try up to 10 fetches, following redirects.
116116
urlstr := *urlFlag
117-
for i := 0; i < 10; i++ {
117+
for range 10 {
118118
// Prepare request.
119119
u, err := url.Parse(urlstr)
120120
if err != nil {

cmd/goimports/goimports.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,8 @@ func replaceTempFilename(diff []byte, filename string) ([]byte, error) {
361361
}
362362
// Always print filepath with slash separator.
363363
f := filepath.ToSlash(filename)
364-
bs[0] = []byte(fmt.Sprintf("--- %s%s", f+".orig", t0))
365-
bs[1] = []byte(fmt.Sprintf("+++ %s%s", f, t1))
364+
bs[0] = fmt.Appendf(nil, "--- %s%s", f+".orig", t0)
365+
bs[1] = fmt.Appendf(nil, "+++ %s%s", f, t1)
366366
return bytes.Join(bs, []byte{'\n'}), nil
367367
}
368368

cmd/goyacc/yacc.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,7 +1478,7 @@ func symnam(i int) string {
14781478

14791479
// set elements 0 through n-1 to c
14801480
func aryfil(v []int, n, c int) {
1481-
for i := 0; i < n; i++ {
1481+
for i := range n {
14821482
v[i] = c
14831483
}
14841484
}
@@ -1840,7 +1840,7 @@ func closure(i int) {
18401840

18411841
nexts:
18421842
// initially fill the sets
1843-
for s := 0; s < n; s++ {
1843+
for s := range n {
18441844
prd := curres[s]
18451845

18461846
//
@@ -2609,7 +2609,7 @@ func callopt() {
26092609
if adb > 2 {
26102610
for p = 0; p <= maxa; p += 10 {
26112611
fmt.Fprintf(ftable, "%v ", p)
2612-
for i = 0; i < 10; i++ {
2612+
for i = range 10 {
26132613
fmt.Fprintf(ftable, "%v ", amem[p+i])
26142614
}
26152615
ftable.WriteRune('\n')
@@ -2653,7 +2653,7 @@ func gin(i int) {
26532653

26542654
// now, find amem place for it
26552655
nextgp:
2656-
for p := 0; p < ACTSIZE; p++ {
2656+
for p := range ACTSIZE {
26572657
if amem[p] != 0 {
26582658
continue
26592659
}
@@ -3117,7 +3117,7 @@ func aryeq(a []int, b []int) int {
31173117
if len(b) != n {
31183118
return 0
31193119
}
3120-
for ll := 0; ll < n; ll++ {
3120+
for ll := range n {
31213121
if a[ll] != b[ll] {
31223122
return 0
31233123
}

cmd/html2article/conv.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"net/url"
1717
"os"
1818
"regexp"
19+
"slices"
1920
"strings"
2021

2122
"golang.org/x/net/html"
@@ -270,10 +271,8 @@ func hasClass(name string) selector {
270271
return func(n *html.Node) bool {
271272
for _, a := range n.Attr {
272273
if a.Key == "class" {
273-
for _, c := range strings.Fields(a.Val) {
274-
if c == name {
275-
return true
276-
}
274+
if slices.Contains(strings.Fields(a.Val), name) {
275+
return true
277276
}
278277
}
279278
}

cmd/present/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ func main() {
7373

7474
origin := &url.URL{Scheme: "http"}
7575
if *originHost != "" {
76-
if strings.HasPrefix(*originHost, "https://") {
77-
*originHost = strings.TrimPrefix(*originHost, "https://")
76+
if after, ok := strings.CutPrefix(*originHost, "https://"); ok {
77+
*originHost = after
7878
origin.Scheme = "https"
7979
}
8080
*originHost = strings.TrimPrefix(*originHost, "http://")

cmd/present2md/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,10 +447,10 @@ func parseInlineLink(s string) (link string, length int) {
447447
// If the URL is http://foo.com, drop the http://
448448
// In other words, render [[http://golang.org]] as:
449449
// <a href="http://golang.org">golang.org</a>
450-
if strings.HasPrefix(rawURL, url.Scheme+"://") {
451-
simpleURL = strings.TrimPrefix(rawURL, url.Scheme+"://")
452-
} else if strings.HasPrefix(rawURL, url.Scheme+":") {
453-
simpleURL = strings.TrimPrefix(rawURL, url.Scheme+":")
450+
if after, ok := strings.CutPrefix(rawURL, url.Scheme+"://"); ok {
451+
simpleURL = after
452+
} else if after, ok := strings.CutPrefix(rawURL, url.Scheme+":"); ok {
453+
simpleURL = after
454454
}
455455
}
456456
return renderLink(rawURL, simpleURL), end + 2

cmd/signature-fuzzer/internal/fuzz-generator/gen_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func mkGenState() *genstate {
3535
func TestBasic(t *testing.T) {
3636
checkTunables(tunables)
3737
s := mkGenState()
38-
for i := 0; i < 1000; i++ {
38+
for i := range 1000 {
3939
s.wr = NewWrapRand(int64(i), RandCtlChecks|RandCtlPanic)
4040
fp := s.GenFunc(i, i)
4141
var buf bytes.Buffer
@@ -58,7 +58,7 @@ func TestMoreComplicated(t *testing.T) {
5858

5959
checkTunables(tunables)
6060
s := mkGenState()
61-
for i := 0; i < 10000; i++ {
61+
for i := range 10000 {
6262
s.wr = NewWrapRand(int64(i), RandCtlChecks|RandCtlPanic)
6363
fp := s.GenFunc(i, i)
6464
var buf bytes.Buffer

cmd/signature-fuzzer/internal/fuzz-generator/generator.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import (
4848
"os"
4949
"os/exec"
5050
"path/filepath"
51+
"slices"
5152
"strconv"
5253
"strings"
5354
)
@@ -561,12 +562,7 @@ func (s *genstate) popTunables() {
561562
// See precludeSelectedTypes below for more info.
562563
func (s *genstate) redistributeFraction(toIncorporate uint8, avoid []int) {
563564
inavoid := func(j int) bool {
564-
for _, k := range avoid {
565-
if j == k {
566-
return true
567-
}
568-
}
569-
return false
565+
return slices.Contains(avoid, j)
570566
}
571567

572568
doredis := func() {
@@ -631,7 +627,7 @@ func (s *genstate) GenParm(f *funcdef, depth int, mkctl bool, pidx int) parm {
631627
// Convert tf into a cumulative sum
632628
tf := s.tunables.typeFractions
633629
sum := uint8(0)
634-
for i := 0; i < len(tf); i++ {
630+
for i := range len(tf) {
635631
sum += tf[i]
636632
tf[i] = sum
637633
}
@@ -662,7 +658,7 @@ func (s *genstate) GenParm(f *funcdef, depth int, mkctl bool, pidx int) parm {
662658
f.structdefs = append(f.structdefs, sp)
663659
tnf := int64(s.tunables.nStructFields) / int64(depth+1)
664660
nf := int(s.wr.Intn(tnf))
665-
for fi := 0; fi < nf; fi++ {
661+
for range nf {
666662
fp := s.GenParm(f, depth+1, false, pidx)
667663
skComp := tunables.doSkipCompare &&
668664
uint8(s.wr.Intn(100)) < s.tunables.skipCompareFraction
@@ -832,7 +828,7 @@ func (s *genstate) GenFunc(fidx int, pidx int) *funcdef {
832828
needControl := f.recur
833829
f.dodefc = uint8(s.wr.Intn(100))
834830
pTaken := uint8(s.wr.Intn(100)) < s.tunables.takenFraction
835-
for pi := 0; pi < numParams; pi++ {
831+
for range numParams {
836832
newparm := s.GenParm(f, 0, needControl, pidx)
837833
if !pTaken {
838834
newparm.SetAddrTaken(notAddrTaken)
@@ -848,7 +844,7 @@ func (s *genstate) GenFunc(fidx int, pidx int) *funcdef {
848844
}
849845

850846
rTaken := uint8(s.wr.Intn(100)) < s.tunables.takenFraction
851-
for ri := 0; ri < numReturns; ri++ {
847+
for range numReturns {
852848
r := s.GenReturn(f, 0, pidx)
853849
if !rTaken {
854850
r.SetAddrTaken(notAddrTaken)
@@ -903,7 +899,7 @@ func (s *genstate) emitCompareFunc(f *funcdef, b *bytes.Buffer, p parm) {
903899
b.WriteString(" return ")
904900
numel := p.NumElements()
905901
ncmp := 0
906-
for i := 0; i < numel; i++ {
902+
for i := range numel {
907903
lelref, lelparm := p.GenElemRef(i, "left")
908904
relref, _ := p.GenElemRef(i, "right")
909905
if lelref == "" || lelref == "_" {
@@ -1501,7 +1497,7 @@ func (s *genstate) emitParamChecks(f *funcdef, b *bytes.Buffer, pidx int, value
15011497
} else {
15021498
numel := p.NumElements()
15031499
cel := checkableElements(p)
1504-
for i := 0; i < numel; i++ {
1500+
for i := range numel {
15051501
verb(4, "emitting check-code for p%d el %d value=%d", pi, i, value)
15061502
elref, elparm := p.GenElemRef(i, s.genParamRef(p, pi))
15071503
valstr, value = s.GenValue(f, elparm, value, false)
@@ -1535,7 +1531,7 @@ func (s *genstate) emitParamChecks(f *funcdef, b *bytes.Buffer, pidx int, value
15351531
// receiver value check
15361532
if f.isMethod {
15371533
numel := f.receiver.NumElements()
1538-
for i := 0; i < numel; i++ {
1534+
for i := range numel {
15391535
verb(4, "emitting check-code for rcvr el %d value=%d", i, value)
15401536
elref, elparm := f.receiver.GenElemRef(i, "rcvr")
15411537
valstr, value = s.GenValue(f, elparm, value, false)
@@ -1608,7 +1604,7 @@ func (s *genstate) emitDeferChecks(f *funcdef, b *bytes.Buffer, value int) int {
16081604
b.WriteString(" // check parm " + which + "\n")
16091605
numel := p.NumElements()
16101606
cel := checkableElements(p)
1611-
for i := 0; i < numel; i++ {
1607+
for i := range numel {
16121608
elref, elparm := p.GenElemRef(i, s.genParamRef(p, pi))
16131609
if elref == "" || elref == "_" || cel == 0 {
16141610
verb(4, "empty skip p%d el %d", pi, i)
@@ -2058,7 +2054,7 @@ func (s *genstate) emitMain(outf *os.File, numit int, fcnmask map[int]int, pkmas
20582054
for k := 0; k < s.NumTestPackages; k++ {
20592055
cp := fmt.Sprintf("%s%s%d", s.Tag, CallerName, k)
20602056
fmt.Fprintf(outf, " go func(ch chan bool) {\n")
2061-
for i := 0; i < numit; i++ {
2057+
for i := range numit {
20622058
if shouldEmitFP(i, k, fcnmask, pkmask) {
20632059
fmt.Fprintf(outf, " %s.%s%d(\"normal\")\n", cp, CallerName, i)
20642060
if s.tunables.doReflectCall {

cmd/stringer/golden_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,6 @@ func TestGolden(t *testing.T) {
453453

454454
dir := t.TempDir()
455455
for _, test := range golden {
456-
test := test
457456
t.Run(test.name, func(t *testing.T) {
458457
input := "package test\n" + test.input
459458
file := test.name + ".go"

0 commit comments

Comments
 (0)