Debian Codegraph @

go/analysis/passes/modernize/stringscutprefix.go

raw symbols SCIP
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 modernizescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/
6
7 import (
8 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
9 "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/"
10 "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/"
11 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
12
13 "golang.org/x/tools/go/analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/"
14 "golang.org/x/tools/go/analysis/passes/inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/"
15 "golang.org/x/tools/go/types/typeutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/"
16 "golang.org/x/tools/internal/analysis/analyzerutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/"
17 typeindexanalyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/typeindex`/ "golang.org/x/tools/internal/analysis/typeindexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/typeindex`/"
18 "golang.org/x/tools/internal/astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/"
19 "golang.org/x/tools/internal/moreitersscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/moreiters`/"
20 "golang.org/x/tools/internal/refactorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/"
21 "golang.org/x/tools/internal/typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/"
22 "golang.org/x/tools/internal/typesinternal/typeindexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/"
23 "golang.org/x/tools/internal/versionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/versions`/"
24 )
25
26 var StringsCutPrefixAnalyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/StringsCutPrefixAnalyzer. = &analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{
27 Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Name.: "stringscutprefix",
28 Docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Doc.: analyzerutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/.MustExtractDocscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/analyzerutil`/MustExtractDoc().(docscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/doc., "stringscutprefix"),
29 Requiresscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Requires.: []*analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#{
30 inspectscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/inspect`/Analyzer.,
31 typeindexanalyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/typeindex`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/typeindex`/Analyzer.,
32 },
33 Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#Run.: stringscutprefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/stringscutprefix().,
34 URLscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Analyzer#URL.: "https://pkg.go.dev/golang.org/x/tools/go/analysis/passes/modernize#stringscutprefix",
35 }
36
37 // stringscutprefix offers a fix to replace an if statement which
38 // calls to the 2 patterns below with strings.CutPrefix or strings.CutSuffix.
39 //
40 // Patterns:
41 //
42 // 1. if strings.HasPrefix(s, pre) { use(strings.TrimPrefix(s, pre) }
43 // =>
44 // if after, ok := strings.CutPrefix(s, pre); ok { use(after) }
45 //
46 // 2. if after := strings.TrimPrefix(s, pre); after != s { use(after) }
47 // =>
48 // if after, ok := strings.CutPrefix(s, pre); ok { use(after) }
49 //
50 // Similar patterns apply for CutSuffix.
51 //
52 // The use must occur within the first statement of the block, and the offered fix
53 // only replaces the first occurrence of strings.TrimPrefix/TrimSuffix.
54 //
55 // Variants:
56 // - bytes.HasPrefix/HasSuffix usage as pattern 1.
57 func stringscutprefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/stringscutprefix().(pass *analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Passscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#) (any, error) {
58 var (
59 index = pass.ResultOfscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#ResultOf.[typeindexanalyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/typeindex`/.Analyzerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/analysis/typeindex`/Analyzer.].(*typeindexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/.Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#)
60 info = pass.TypesInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#TypesInfo.
61
62 stringsTrimPrefix = index.Objectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Object().("strings", "TrimPrefix")
63 bytesTrimPrefix = index.Objectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Object().("bytes", "TrimPrefix")
64 stringsTrimSuffix = index.Objectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Object().("strings", "TrimSuffix")
65 bytesTrimSuffix = index.Objectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Object().("bytes", "TrimSuffix")
66 )
67 if !index.Usedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Used().(stringsTrimPrefix, bytesTrimPrefix, stringsTrimSuffix, bytesTrimSuffix) {
68 return nil, nil
69 }
70
71 for curFile := range filesUsingGoVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/filesUsingGoVersion().(pass, versionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/versions`/.Go1_20scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/versions`/Go1_20.) {
72 for curIfStmt := range curFile.Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Preorder().((*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.IfStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#)(nil)) {
73 ifStmt := curIfStmt.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.IfStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#)
74
75 // pattern1
76 if call, ok := ifStmt.Condscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Cond..(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#); ok && ifStmt.Initscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Init. == nil && len(ifStmt.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Body..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#List.) > 0 {
77
78 obj := typeutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/.Calleescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/Callee().(info, call)
79 if !typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.IsFunctionNamedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/IsFunctionNamed().(obj, "strings", "HasPrefix", "HasSuffix") &&
80 !typesinternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/.IsFunctionNamedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal`/IsFunctionNamed().(obj, "bytes", "HasPrefix", "HasSuffix") {
81 continue
82 }
83 isPrefix := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(obj.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Name().(), "Prefix")
84
85 // Replace the first occurrence of strings.TrimPrefix(s, pre) in the first statement only,
86 // but not later statements in case s or pre are modified by intervening logic (ditto Suffix).
87 firstStmt := curIfStmt.Childscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Child().(ifStmt.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Body.).Childscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Child().(ifStmt.Bodyscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Body..Listscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BlockStmt#List.[0])
88 for curCall := range firstStmt.Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Preorder().((*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#)(nil)) {
89 call1 := curCall.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#)
90 obj1 := typeutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/.Calleescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/Callee().(info, call1)
91 // bytesTrimPrefix or stringsTrimPrefix might be nil if the file doesn't import it,
92 // so we need to ensure the obj1 is not nil otherwise the call1 is not TrimPrefix and cause a panic (ditto Suffix).
93 if obj1 == nil ||
94 obj1 != stringsTrimPrefix && obj1 != bytesTrimPrefix &&
95 obj1 != stringsTrimSuffix && obj1 != bytesTrimSuffix {
96 continue
97 }
98
99 isPrefix1 := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(obj1.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Name().(), "Prefix")
100 var cutFuncName, varName, message, fixMessage string
101 if isPrefix && isPrefix1 {
102 cutFuncName = "CutPrefix"
103 varName = "after"
104 message = "HasPrefix + TrimPrefix can be simplified to CutPrefix"
105 fixMessage = "Replace HasPrefix/TrimPrefix with CutPrefix"
106 } else if !isPrefix && !isPrefix1 {
107 cutFuncName = "CutSuffix"
108 varName = "before"
109 message = "HasSuffix + TrimSuffix can be simplified to CutSuffix"
110 fixMessage = "Replace HasSuffix/TrimSuffix with CutSuffix"
111 } else {
112 continue
113 }
114
115 // Have: if strings.HasPrefix(s0, pre0) { ...strings.TrimPrefix(s, pre)... } (ditto Suffix)
116 var (
117 s0 = call.Argsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Args.[0]
118 pre0 = call.Argsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Args.[1]
119 s = call1.Argsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Args.[0]
120 pre = call1.Argsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Args.[1]
121 )
122
123 // check whether the obj1 uses the exact the same argument with strings.HasPrefix
124 // shadow variables won't be valid because we only access the first statement (ditto Suffix).
125 if astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.EqualSyntaxscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/EqualSyntax().(s0, s) && astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.EqualSyntaxscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/EqualSyntax().(pre0, pre) {
126 after := refactorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/.FreshNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/FreshName().(info.Scopesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Scopes.[ifStmt], ifStmt.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Pos().(), varName)
127 prefix, importEdits := refactorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/.AddImportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/AddImport().(
128 info,
129 curFile.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#),
130 obj1.Pkgscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Pkg().().Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Name().(),
131 obj1.Pkgscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Pkg().().Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Path().(),
132 cutFuncName,
133 call.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Pos().(),
134 )
135 okVarName := refactorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/.FreshNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/FreshName().(info.Scopesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Scopes.[ifStmt], ifStmt.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Pos().(), "ok")
136 pass.Reportscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Report.(analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Diagnosticscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#{
137 // highlight at HasPrefix call (ditto Suffix).
138 Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#Pos.: call.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Pos().(),
139 Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#End.: call.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#End().(),
140 Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#Message.: message,
141 SuggestedFixesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#SuggestedFixes.: []analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.SuggestedFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#{{
142 Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#Message.: fixMessage,
143 // if strings.HasPrefix(s, pre) { use(strings.TrimPrefix(s, pre)) }
144 // ------------ ----------------- ----- --------------------------
145 // if after, ok := strings.CutPrefix(s, pre); ok { use(after) }
146 // (ditto Suffix)
147 TextEditsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#TextEdits.: append(importEdits, []analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.TextEditscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#{
148 {
149 Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: call.Funscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Fun..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(),
150 Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: call.Funscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Fun..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(),
151 NewTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#NewText.: fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Appendfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Appendf().(nil, "%s, %s :=", after, okVarName),
152 },
153 {
154 Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: call.Funscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Fun..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(),
155 Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: call.Funscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Fun..Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().(),
156 NewTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#NewText.: fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Appendfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Appendf().(nil, "%s%s", prefix, cutFuncName),
157 },
158 {
159 Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: call.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#End().(),
160 Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: call.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#End().(),
161 NewTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#NewText.: fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Appendfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Appendf().(nil, "; %s ", okVarName),
162 },
163 {
164 Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: call1.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Pos().(),
165 Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: call1.Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#End().(),
166 NewTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#NewText.: []byte(after),
167 },
168 }...),
169 }}},
170 )
171 break
172 }
173 }
174 }
175
176 // pattern2
177 if bin, ok := ifStmt.Condscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Cond..(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.BinaryExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BinaryExpr#); ok &&
178 bin.Opscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BinaryExpr#Op. == tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NEQscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NEQ. &&
179 ifStmt.Initscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Init. != nil &&
180 isSimpleAssignscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/isSimpleAssign().(ifStmt.Initscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Init.) {
181 assign := ifStmt.Initscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Init..(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.AssignStmtscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#)
182 if call, ok := assign.Rhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Rhs.[0].(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.CallExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#); ok && assign.Tokscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Tok. == tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.DEFINEscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/DEFINE. {
183 lhs := assign.Lhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Lhs.[0]
184 obj := typeutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/.Calleescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/types/typeutil`/Callee().(info, call)
185
186 if obj == nil ||
187 obj != stringsTrimPrefix && obj != bytesTrimPrefix && obj != stringsTrimSuffix && obj != bytesTrimSuffix {
188 continue
189 }
190
191 isPrefix1 := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(obj.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Name().(), "Prefix")
192 var cutFuncName, message, fixMessage string
193 if isPrefix1 {
194 cutFuncName = "CutPrefix"
195 message = "TrimPrefix can be simplified to CutPrefix"
196 fixMessage = "Replace TrimPrefix with CutPrefix"
197 } else {
198 cutFuncName = "CutSuffix"
199 message = "TrimSuffix can be simplified to CutSuffix"
200 fixMessage = "Replace TrimSuffix with CutSuffix"
201 }
202
203 if astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.EqualSyntaxscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/EqualSyntax().(lhs, bin.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BinaryExpr#X.) && astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.EqualSyntaxscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/EqualSyntax().(call.Argsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Args.[0], bin.Yscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BinaryExpr#Y.) ||
204 (astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.EqualSyntaxscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/EqualSyntax().(lhs, bin.Yscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BinaryExpr#Y.) && astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/.EqualSyntaxscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/astutil`/EqualSyntax().(call.Argsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Args.[0], bin.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BinaryExpr#X.)) {
205 okVarName := freshNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis/passes/modernize`/freshName().(info, index, info.Scopesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Scopes.[ifStmt], ifStmt.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Pos().(), curIfStmt, curIfStmt, tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NoPosscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NoPos., "ok")
206 // Have one of:
207 // if rest := TrimPrefix(s, prefix); rest != s { (ditto Suffix)
208 // if rest := TrimPrefix(s, prefix); s != rest { (ditto Suffix)
209
210 // We use AddImport not to add an import (since it exists already)
211 // but to compute the correct prefix in the dot-import case.
212 prefix, importEdits := refactorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/.AddImportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/refactor`/AddImport().(
213 info,
214 curFile.Nodescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Cursor#Node().().(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#),
215 obj.Pkgscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Pkg().().Namescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Name().(),
216 obj.Pkgscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Object#Pkg().().Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Package#Path().(),
217 cutFuncName,
218 call.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Pos().(),
219 )
220
221 // if x := strings.TrimPrefix(s, pre); x != s ...
222 // ---- ---------- ------
223 // if x, ok := strings.CutPrefix (s, pre); ok ...
224 // (ditto Suffix)
225 edits := append(importEdits, []analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.TextEditscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#{
226 {
227 Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: assign.Lhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Lhs.[0].Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().(),
228 Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: assign.Lhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Lhs.[0].Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().(),
229 NewTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#NewText.: fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Appendfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Appendf().(nil, ", %s", okVarName),
230 },
231 {
232 Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: call.Funscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Fun..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(),
233 Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: call.Funscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/CallExpr#Fun..Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().(),
234 NewTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#NewText.: fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Appendfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Appendf().(nil, "%s%s", prefix, cutFuncName),
235 },
236 {
237 Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: ifStmt.Condscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Cond..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(),
238 Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: ifStmt.Condscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Cond..Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().(),
239 NewTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#NewText.: []byte(okVarName),
240 },
241 }...)
242
243 // Replace the "after" variable with "_" if is unused inside the if statement.
244 if id, ok := lhs.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#); ok {
245 if obj := info.ObjectOfscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#ObjectOf().(id); obj != nil && moreitersscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/moreiters`/.Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/moreiters`/Len().(index.Usesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typesinternal/typeindex`/Index#Uses().(obj)) < 2 {
246 edits = append(edits, analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.TextEditscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#{
247 Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#Pos.: assign.Lhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Lhs.[0].Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(),
248 Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#End.: assign.Lhsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/AssignStmt#Lhs.[0].Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().(),
249 NewTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/TextEdit#NewText.: []byte("_"),
250 })
251 }
252 }
253
254 pass.Reportscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Pass#Report.(analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.Diagnosticscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#{
255 // highlight from the init and the condition end.
256 Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#Pos.: ifStmt.Initscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Init..Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#Pos().(),
257 Endscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#End.: ifStmt.Condscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IfStmt#Cond..Endscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#End().(),
258 Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#Message.: message,
259 SuggestedFixesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/Diagnostic#SuggestedFixes.: []analysisscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/.SuggestedFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#{{
260 Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#Message.: fixMessage,
261 TextEditsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/analysis`/SuggestedFix#TextEdits.: edits,
262 }},
263 })
264 }
265 }
266 }
267 }
268 }
269 return nil, nil
270 }