github.com/amarpal/go-tools@v0.0.0-20240422043104-40142f59f616/staticcheck/sa6003/sa6003.go (about)

     1  package sa6003
     2  
     3  import (
     4  	"github.com/amarpal/go-tools/analysis/lint"
     5  	"github.com/amarpal/go-tools/internal/passes/buildir"
     6  	"github.com/amarpal/go-tools/internal/sharedcheck"
     7  
     8  	"golang.org/x/tools/go/analysis"
     9  )
    10  
    11  var SCAnalyzer = lint.InitializeAnalyzer(&lint.Analyzer{
    12  	Analyzer: &analysis.Analyzer{
    13  		Name:     "SA6003",
    14  		Run:      sharedcheck.CheckRangeStringRunes,
    15  		Requires: []*analysis.Analyzer{buildir.Analyzer},
    16  	},
    17  	Doc: &lint.Documentation{
    18  		Title: `Converting a string to a slice of runes before ranging over it`,
    19  		Text: `You may want to loop over the runes in a string. Instead of converting
    20  the string to a slice of runes and looping over that, you can loop
    21  over the string itself. That is,
    22  
    23      for _, r := range s {}
    24  
    25  and
    26  
    27      for _, r := range []rune(s) {}
    28  
    29  will yield the same values. The first version, however, will be faster
    30  and avoid unnecessary memory allocations.
    31  
    32  Do note that if you are interested in the indices, ranging over a
    33  string and over a slice of runes will yield different indices. The
    34  first one yields byte offsets, while the second one yields indices in
    35  the slice of runes.`,
    36  		Since:    "2017.1",
    37  		Severity: lint.SeverityWarning,
    38  		MergeIf:  lint.MergeIfAny,
    39  	},
    40  })
    41  
    42  var Analyzer = SCAnalyzer.Analyzer