43 lines
1.2 KiB
Go
43 lines
1.2 KiB
Go
package sa6003
|
|
|
|
import (
|
|
"honnef.co/go/tools/analysis/lint"
|
|
"honnef.co/go/tools/internal/passes/buildir"
|
|
"honnef.co/go/tools/internal/sharedcheck"
|
|
|
|
"golang.org/x/tools/go/analysis"
|
|
)
|
|
|
|
var SCAnalyzer = lint.InitializeAnalyzer(&lint.Analyzer{
|
|
Analyzer: &analysis.Analyzer{
|
|
Name: "SA6003",
|
|
Run: sharedcheck.CheckRangeStringRunes,
|
|
Requires: []*analysis.Analyzer{buildir.Analyzer},
|
|
},
|
|
Doc: &lint.RawDocumentation{
|
|
Title: `Converting a string to a slice of runes before ranging over it`,
|
|
Text: `You may want to loop over the runes in a string. Instead of converting
|
|
the string to a slice of runes and looping over that, you can loop
|
|
over the string itself. That is,
|
|
|
|
for _, r := range s {}
|
|
|
|
and
|
|
|
|
for _, r := range []rune(s) {}
|
|
|
|
will yield the same values. The first version, however, will be faster
|
|
and avoid unnecessary memory allocations.
|
|
|
|
Do note that if you are interested in the indices, ranging over a
|
|
string and over a slice of runes will yield different indices. The
|
|
first one yields byte offsets, while the second one yields indices in
|
|
the slice of runes.`,
|
|
Since: "2017.1",
|
|
Severity: lint.SeverityWarning,
|
|
MergeIf: lint.MergeIfAny,
|
|
},
|
|
})
|
|
|
|
var Analyzer = SCAnalyzer.Analyzer
|