aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/ckaznocha
diff options
context:
space:
mode:
authorTaras Madan <tarasmadan@google.com>2024-11-11 11:41:38 +0100
committerTaras Madan <tarasmadan@google.com>2024-11-11 11:10:48 +0000
commit27e76fae2ee2d84dc7db63af1d9ed7358ba35b7a (patch)
treeed19c0e35e272b3c4cc5a2f2c595e035b2428337 /vendor/github.com/ckaznocha
parent621e84e063b0e15b23e17780338627c509e1b9e8 (diff)
vendor: update
Diffstat (limited to 'vendor/github.com/ckaznocha')
-rw-r--r--vendor/github.com/ckaznocha/intrange/intrange.go60
1 files changed, 44 insertions, 16 deletions
diff --git a/vendor/github.com/ckaznocha/intrange/intrange.go b/vendor/github.com/ckaznocha/intrange/intrange.go
index 33cddf303..44a15091e 100644
--- a/vendor/github.com/ckaznocha/intrange/intrange.go
+++ b/vendor/github.com/ckaznocha/intrange/intrange.go
@@ -25,8 +25,9 @@ var (
)
const (
- msg = "for loop can be changed to use an integer range (Go 1.22+)"
- msgLenRange = "for loop can be changed to `i := range %s`"
+ msg = "for loop can be changed to use an integer range (Go 1.22+)"
+ msgLenRange = "for loop can be changed to `%s := range %s`"
+ msgLenRangeNoIdent = "for loop can be changed to `range %s`"
)
func run(pass *analysis.Pass) (any, error) {
@@ -243,21 +244,26 @@ func checkForStmt(pass *analysis.Pass, forStmt *ast.ForStmt) {
}
func checkRangeStmt(pass *analysis.Pass, rangeStmt *ast.RangeStmt) {
- if rangeStmt.Key == nil {
+ if rangeStmt.Value != nil {
return
}
- ident, ok := rangeStmt.Key.(*ast.Ident)
- if !ok {
- return
- }
+ startPos := rangeStmt.Range
+ usesKey := rangeStmt.Key != nil
+ identName := ""
- if ident.Name == "_" {
- return
- }
+ if usesKey {
+ ident, ok := rangeStmt.Key.(*ast.Ident)
+ if !ok {
+ return
+ }
- if rangeStmt.Value != nil {
- return
+ if ident.Name == "_" {
+ usesKey = false
+ }
+
+ identName = ident.Name
+ startPos = ident.Pos()
}
if rangeStmt.X == nil {
@@ -295,18 +301,40 @@ func checkRangeStmt(pass *analysis.Pass, rangeStmt *ast.RangeStmt) {
return
}
+ if usesKey {
+ pass.Report(analysis.Diagnostic{
+ Pos: startPos,
+ End: x.End(),
+ Message: fmt.Sprintf(msgLenRange, identName, arg.Name),
+ SuggestedFixes: []analysis.SuggestedFix{
+ {
+ Message: fmt.Sprintf("Replace `len(%s)` with `%s`", arg.Name, arg.Name),
+ TextEdits: []analysis.TextEdit{
+ {
+ Pos: x.Pos(),
+ End: x.End(),
+ NewText: []byte(arg.Name),
+ },
+ },
+ },
+ },
+ })
+
+ return
+ }
+
pass.Report(analysis.Diagnostic{
- Pos: ident.Pos(),
+ Pos: startPos,
End: x.End(),
- Message: fmt.Sprintf(msgLenRange, arg.Name),
+ Message: fmt.Sprintf(msgLenRangeNoIdent, arg.Name),
SuggestedFixes: []analysis.SuggestedFix{
{
Message: fmt.Sprintf("Replace `len(%s)` with `%s`", arg.Name, arg.Name),
TextEdits: []analysis.TextEdit{
{
- Pos: x.Pos(),
+ Pos: startPos,
End: x.End(),
- NewText: []byte(arg.Name),
+ NewText: []byte(fmt.Sprintf("range %s", arg.Name)),
},
},
},