| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package writestring |
| 8 |
|
| 9 |
import ( |
| 10 |
_ "embed" |
| 11 |
"fmt" |
| 12 |
"go/ast" |
| 13 |
"go/token" |
| 14 |
"go/types" |
| 15 |
"strings" |
| 16 |
|
| 17 |
"golang.org/x/tools/go/analysis" |
| 18 |
"golang.org/x/tools/go/analysis/passes/inspect" |
| 19 |
"golang.org/x/tools/go/ast/edge" |
| 20 |
"golang.org/x/tools/go/ast/inspector" |
| 21 |
"golang.org/x/tools/go/types/typeutil" |
| 22 |
"golang.org/x/tools/internal/analysis/analyzerutil" |
| 23 |
"golang.org/x/tools/internal/astutil" |
| 24 |
"golang.org/x/tools/internal/typesinternal" |
| 25 |
) |
| 26 |
|
| 27 |
|
| 28 |
var doc string |
| 29 |
|
| 30 |
var Analyzer = &analysis.Analyzer{ |
| 31 |
Name: "writestring", |
| 32 |
Doc: analyzerutil.MustExtractDoc(doc, "writestring"), |
| 33 |
URL: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/writestring", |
| 34 |
Requires: []*analysis.Analyzer{inspect.Analyzer}, |
| 35 |
Run: run, |
| 36 |
} |
| 37 |
|
| 38 |
func run(pass *analysis.Pass) (any, error) { |
| 39 |
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) |
| 40 |
|
| 41 |
for curCall := range inspect.Root().Preorder((*ast.CallExpr)(nil)) { |
| 42 |
call := curCall.Node().(*ast.CallExpr) |
| 43 |
info := pass.TypesInfo |
| 44 |
callee := typeutil.Callee(info, call) |
| 45 |
if callee == nil || callee.Name() != "WriteString" { |
| 46 |
continue |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
if !(typesinternal.IsMethodNamed(callee, "strings", "Builder", "WriteString") || |
| 57 |
typesinternal.IsMethodNamed(callee, "bytes", "Buffer", "WriteString") || |
| 58 |
typesinternal.IsMethodNamed(callee, "bufio", "Writer", "WriteString") || |
| 59 |
typesinternal.IsMethodNamed(callee, "hash/maphash", "Hash", "WriteString")) { |
| 60 |
continue |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
if curCall.ParentEdgeKind() != edge.ExprStmt_X { |
| 67 |
continue |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
if !typesinternal.NoEffects(info, call.Fun) { |
| 77 |
continue |
| 78 |
} |
| 79 |
|
| 80 |
if len(call.Args) != 1 { |
| 81 |
continue |
| 82 |
} |
| 83 |
arg := call.Args[0] |
| 84 |
if _, ok := arg.(*ast.BinaryExpr); !ok { |
| 85 |
continue |
| 86 |
} |
| 87 |
|
| 88 |
operands := stringConcatenands(info, arg) |
| 89 |
if len(operands) < 2 { |
| 90 |
continue |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
var newStmts []string |
| 95 |
for _, operand := range operands { |
| 96 |
stmt := fmt.Sprintf("%s(%s)", |
| 97 |
astutil.Format(pass.Fset, call.Fun), |
| 98 |
astutil.Format(pass.Fset, operand)) |
| 99 |
newStmts = append(newStmts, stmt) |
| 100 |
} |
| 101 |
replacement := strings.Join(newStmts, ";") |
| 102 |
|
| 103 |
pass.Report(analysis.Diagnostic{ |
| 104 |
Pos: call.Pos(), |
| 105 |
End: call.End(), |
| 106 |
Message: "Inefficient string concatenation in call to WriteString", |
| 107 |
SuggestedFixes: []analysis.SuggestedFix{{ |
| 108 |
Message: "Split into separate WriteString calls", |
| 109 |
TextEdits: []analysis.TextEdit{{ |
| 110 |
Pos: call.Pos(), |
| 111 |
End: call.End(), |
| 112 |
NewText: []byte(replacement), |
| 113 |
}}, |
| 114 |
}}, |
| 115 |
}) |
| 116 |
} |
| 117 |
return nil, nil |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
func stringConcatenands(info *types.Info, expr ast.Expr) (operands []ast.Expr) { |
| 128 |
if info.Types[expr].Value != nil { |
| 129 |
return []ast.Expr{expr} |
| 130 |
} |
| 131 |
if bin, ok := ast.Unparen(expr).(*ast.BinaryExpr); ok { |
| 132 |
if bin.Op != token.ADD { |
| 133 |
|
| 134 |
|
| 135 |
panic("invalid concatenation operator") |
| 136 |
} |
| 137 |
opsX := stringConcatenands(info, bin.X) |
| 138 |
if opsX == nil { |
| 139 |
return nil |
| 140 |
} |
| 141 |
opsY := stringConcatenands(info, bin.Y) |
| 142 |
if opsY == nil { |
| 143 |
return nil |
| 144 |
} |
| 145 |
return append(opsX, opsY...) |
| 146 |
} |
| 147 |
return []ast.Expr{expr} |
| 148 |
} |