|
| 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 | +package modernize |
| 6 | + |
| 7 | +import ( |
| 8 | + "go/ast" |
| 9 | + "go/token" |
| 10 | + "go/types" |
| 11 | + |
| 12 | + "golang.org/x/tools/go/analysis" |
| 13 | + "golang.org/x/tools/go/analysis/passes/inspect" |
| 14 | + "golang.org/x/tools/go/ast/inspector" |
| 15 | + "golang.org/x/tools/go/types/typeutil" |
| 16 | + "golang.org/x/tools/internal/analysisinternal" |
| 17 | + "golang.org/x/tools/internal/astutil/edge" |
| 18 | +) |
| 19 | + |
| 20 | +// splitseq offers a fix to replace a call to strings.Split with |
| 21 | +// SplitSeq when it is the operand of a range loop, either directly: |
| 22 | +// |
| 23 | +// for _, line := range strings.Split() {...} |
| 24 | +// |
| 25 | +// or indirectly, if the variable's sole use is the range statement: |
| 26 | +// |
| 27 | +// lines := strings.Split() |
| 28 | +// for _, line := range lines {...} |
| 29 | +// |
| 30 | +// Variants: |
| 31 | +// - bytes.SplitSeq |
| 32 | +func splitseq(pass *analysis.Pass) { |
| 33 | + if !analysisinternal.Imports(pass.Pkg, "strings") && |
| 34 | + !analysisinternal.Imports(pass.Pkg, "bytes") { |
| 35 | + return |
| 36 | + } |
| 37 | + info := pass.TypesInfo |
| 38 | + inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) |
| 39 | + for curFile := range filesUsing(inspect, info, "go1.24") { |
| 40 | + for curRange := range curFile.Preorder((*ast.RangeStmt)(nil)) { |
| 41 | + rng := curRange.Node().(*ast.RangeStmt) |
| 42 | + |
| 43 | + // Reject "for i, line := ..." since SplitSeq is not an iter.Seq2. |
| 44 | + // (We require that i is blank.) |
| 45 | + if id, ok := rng.Key.(*ast.Ident); ok && id.Name != "_" { |
| 46 | + continue |
| 47 | + } |
| 48 | + |
| 49 | + // Find the call operand of the range statement, |
| 50 | + // whether direct or indirect. |
| 51 | + call, ok := rng.X.(*ast.CallExpr) |
| 52 | + if !ok { |
| 53 | + if id, ok := rng.X.(*ast.Ident); ok { |
| 54 | + if v, ok := info.Uses[id].(*types.Var); ok { |
| 55 | + if ek, idx := curRange.Edge(); ek == edge.BlockStmt_List && idx > 0 { |
| 56 | + curPrev, _ := curRange.PrevSibling() |
| 57 | + if assign, ok := curPrev.Node().(*ast.AssignStmt); ok && |
| 58 | + assign.Tok == token.DEFINE && |
| 59 | + len(assign.Lhs) == 1 && |
| 60 | + len(assign.Rhs) == 1 && |
| 61 | + info.Defs[assign.Lhs[0].(*ast.Ident)] == v && |
| 62 | + soleUse(info, v) == id { |
| 63 | + // Have: |
| 64 | + // lines := ... |
| 65 | + // for _, line := range lines {...} |
| 66 | + // and no other uses of lines. |
| 67 | + call, _ = assign.Rhs[0].(*ast.CallExpr) |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if call != nil { |
| 75 | + var edits []analysis.TextEdit |
| 76 | + if rng.Key != nil { |
| 77 | + // Delete (blank) RangeStmt.Key: |
| 78 | + // for _, line := -> for line := |
| 79 | + // for _, _ := -> for |
| 80 | + // for _ := -> for |
| 81 | + end := rng.Range |
| 82 | + if rng.Value != nil { |
| 83 | + end = rng.Value.Pos() |
| 84 | + } |
| 85 | + edits = append(edits, analysis.TextEdit{ |
| 86 | + Pos: rng.Key.Pos(), |
| 87 | + End: end, |
| 88 | + }) |
| 89 | + } |
| 90 | + |
| 91 | + if sel, ok := call.Fun.(*ast.SelectorExpr); ok && |
| 92 | + (analysisinternal.IsFunctionNamed(typeutil.Callee(info, call), "strings", "Split") || |
| 93 | + analysisinternal.IsFunctionNamed(typeutil.Callee(info, call), "bytes", "Split")) { |
| 94 | + pass.Report(analysis.Diagnostic{ |
| 95 | + Pos: sel.Pos(), |
| 96 | + End: sel.End(), |
| 97 | + Category: "splitseq", |
| 98 | + Message: "Ranging over SplitSeq is more efficient", |
| 99 | + SuggestedFixes: []analysis.SuggestedFix{{ |
| 100 | + Message: "Replace Split with SplitSeq", |
| 101 | + TextEdits: append(edits, analysis.TextEdit{ |
| 102 | + // Split -> SplitSeq |
| 103 | + Pos: sel.Sel.Pos(), |
| 104 | + End: sel.Sel.End(), |
| 105 | + NewText: []byte("SplitSeq")}), |
| 106 | + }}, |
| 107 | + }) |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments