Source file src/cmd/fix/main.go

     1  // Copyright 2025 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  /*
     6  Fix is a tool executed by "go fix" to update Go programs that use old
     7  features of the language and library and rewrite them to use newer
     8  ones. After you update to a new Go release, fix helps make the
     9  necessary changes to your programs.
    10  
    11  See the documentation for "go fix" for how to run this command.
    12  You can provide an alternative tool using "go fix -fixtool=..."
    13  
    14  Run "go tool fix help" to see the list of analyzers supported by this
    15  program.
    16  
    17  See [golang.org/x/tools/go/analysis] for information on how to write
    18  an analyzer that can suggest fixes.
    19  */
    20  package main
    21  
    22  import (
    23  	"cmd/internal/objabi"
    24  	"cmd/internal/telemetry/counter"
    25  	"slices"
    26  
    27  	"golang.org/x/tools/go/analysis"
    28  	"golang.org/x/tools/go/analysis/passes/buildtag"
    29  	"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"
    32  	"golang.org/x/tools/go/analysis/unitchecker"
    33  )
    34  
    35  func main() {
    36  	// Keep consistent with cmd/vet/main.go!
    37  	counter.Open()
    38  	objabi.AddVersionFlag()
    39  	counter.Inc("fix/invocations")
    40  
    41  	unitchecker.Main(suite...) // (never returns)
    42  }
    43  
    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.
    54  	// 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.)
    57  	// Rejected:
    58  	// - composites: some types (e.g. PointXY{1,2}) don't want field names.
    59  	// - timeformat: flipping MM/DD is a behavior change, but the code
    60  	//    could potentially be a workaround for another bug.
    61  	// - stringintconv: offers two fixes, user input required to choose.
    62  	// - fieldalignment: poor signal/noise; fix could be a regression.
    63  )
    64  

View as plain text