@@ -476,39 +476,80 @@ Package documentation: [lostcancel](https://pkg.go.dev/golang.org/x/tools/go/ana
476
476
477
477
478
478
This analyzer reports opportunities for simplifying and clarifying
479
- existing code by using more modern features of Go, such as:
480
-
481
- - replacing an if/else conditional assignment by a call to the
482
- built-in min or max functions added in go1.21;
483
- - replacing sort.Slice(x, func(i, j int) bool) { return s[ i] < s[ j] }
484
- by a call to slices.Sort(s), added in go1.21;
485
- - replacing interface{} by the 'any' type added in go1.18;
486
- - replacing append([ ] T(nil), s...) by slices.Clone(s) or
487
- slices.Concat(s), added in go1.21;
488
- - replacing a loop around an m[ k] =v map update by a call
489
- to one of the Collect, Copy, Clone, or Insert functions
490
- from the maps package, added in go1.21;
491
- - replacing [ ] byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),
492
- added in go1.19;
493
- - replacing uses of context.WithCancel in tests with t.Context, added in
494
- go1.24;
495
- - replacing omitempty by omitzero on structs, added in go1.24;
496
- - replacing append(s[ : i ] , s[ i+1] ...) by slices.Delete(s, i, i+1),
497
- added in go1.21
498
- - replacing a 3-clause for i := 0; i < n; i++ {} loop by
499
- for i := range n {}, added in go1.22;
500
- - replacing Split in "for range strings.Split(...)" by go1.24's
501
- more efficient SplitSeq, or Fields with FieldSeq;
479
+ existing code by using more modern features of Go and its standard
480
+ library.
481
+
482
+ Each diagnostic provides a fix. Our intent is that these fixes may
483
+ be safely applied en masse without changing the behavior of your
484
+ program. In some cases the suggested fixes are imperfect and may
485
+ lead to (for example) unused imports or unused local variables,
486
+ causing build breakage. However, these problems are generally
487
+ trivial to fix. We regard any modernizer whose fix changes program
488
+ behavior to have a serious bug and will endeavor to fix it.
502
489
503
490
To apply all modernization fixes en masse, you can use the
504
491
following command:
505
492
506
- $ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -test ./...
493
+ $ go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix - test ./...
507
494
508
495
If the tool warns of conflicting fixes, you may need to run it more
509
496
than once until it has applied all fixes cleanly. This command is
510
497
not an officially supported interface and may change in the future.
511
498
499
+ Changes produced by this tool should be reviewed as usual before
500
+ being merged. In some cases, a loop may be replaced by a simple
501
+ function call, causing comments within the loop to be discarded.
502
+ Human judgment may be required to avoid losing comments of value.
503
+
504
+ Each diagnostic reported by modernize has a specific category. (The
505
+ categories are listed below.) Diagnostics in some categories, such
506
+ as "efaceany" (which replaces "interface{}" with "any" where it is
507
+ safe to do so) are particularly numerous. It may ease the burden of
508
+ code review to apply fixes in two passes, the first change
509
+ consisting only of fixes of category "efaceany", the second
510
+ consisting of all others. This can be achieved using the -category flag:
511
+
512
+ $ modernize -category=efaceany -fix -test ./...
513
+ $ modernize -category=-efaceany -fix -test ./...
514
+
515
+ Categories of modernize diagnostic:
516
+
517
+ - minmax: replace an if/else conditional assignment by a call to
518
+ the built-in min or max functions added in go1.21.
519
+
520
+ - sortslice: replace sort.Slice(x, func(i, j int) bool) { return s[ i] < s[ j] }
521
+ by a call to slices.Sort(s), added in go1.21.
522
+
523
+ - efaceany: replace interface{} by the 'any' type added in go1.18.
524
+
525
+ - slicesclone: replace append([ ] T(nil), s...) by slices.Clone(s) or
526
+ slices.Concat(s), added in go1.21.
527
+
528
+ - mapsloop: replace a loop around an m[ k] =v map update by a call
529
+ to one of the Collect, Copy, Clone, or Insert functions from
530
+ the maps package, added in go1.21.
531
+
532
+ - fmtappendf: replace [ ] byte(fmt.Sprintf...) by fmt.Appendf(nil, ...),
533
+ added in go1.19.
534
+
535
+ - testingcontext: replace uses of context.WithCancel in tests
536
+ with t.Context, added in go1.24.
537
+
538
+ - omitzero: replace omitempty by omitzero on structs, added in go1.24.
539
+
540
+ - bloop: replace "for i := range b.N" or "for range b.N" in a
541
+ benchmark with "for b.Loop()", and remove any preceding calls
542
+ to b.StopTimer, b.StartTimer, and b.ResetTimer.
543
+
544
+ - slicesdelete: replace append(s[ : i ] , s[ i+1] ...) by
545
+ slices.Delete(s, i, i+1), added in go1.21.
546
+
547
+ - rangeint: replace a 3-clause "for i := 0; i < n; i++" loop by
548
+ "for i := range n", added in go1.22.
549
+
550
+ - stringseq: replace Split in "for range strings.Split(...)" by go1.24's
551
+ more efficient SplitSeq, or Fields with FieldSeq.
552
+
512
553
Default: on.
513
554
514
555
Package documentation: [ modernize] ( https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize )
0 commit comments