| 1 |
package stringscut |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"strings" |
| 6 |
) |
| 7 |
|
| 8 |
func basic(s string) bool { |
| 9 |
s = "reassigned" |
| 10 |
i := strings.Index(s, "=") // want "strings.Index can be simplified using strings.Cut" |
| 11 |
if i >= 0 { |
| 12 |
print(s[:i]) |
| 13 |
} |
| 14 |
return i >= 0 |
| 15 |
} |
| 16 |
|
| 17 |
func basic_contains(s string) bool { |
| 18 |
s = "reassigned" |
| 19 |
i := strings.Index(s, "=") // want "strings.Index can be simplified using strings.Contains" |
| 20 |
return i >= 0 |
| 21 |
} |
| 22 |
|
| 23 |
func contains_variety(s, sub string) { |
| 24 |
i := strings.Index(s, sub) // want "strings.Index can be simplified using strings.Contains" |
| 25 |
if i >= 0 { |
| 26 |
print("found") |
| 27 |
} |
| 28 |
if i < 0 { |
| 29 |
print("not found") |
| 30 |
} |
| 31 |
if i <= -1 { |
| 32 |
print("not found") |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
func basic_contains_bytes(s string) bool { |
| 37 |
i := strings.IndexByte(s, '=') // want "strings.IndexByte can be simplified using strings.Contains" |
| 38 |
return i < 0 |
| 39 |
} |
| 40 |
|
| 41 |
func basic_contains_bytes_byte(s []byte) bool { |
| 42 |
i := bytes.IndexByte(s, 22) // want "bytes.IndexByte can be simplified using bytes.Contains" |
| 43 |
return i < 0 |
| 44 |
} |
| 45 |
|
| 46 |
func skip_var_decl(s string) bool { |
| 47 |
var i int |
| 48 |
i = strings.Index(s, "=") // don't modernize - i might be reassigned |
| 49 |
print(s[:i]) |
| 50 |
return i >= 0 |
| 51 |
} |
| 52 |
|
| 53 |
func basic_substr_arg(s string, substr string) bool { |
| 54 |
i := strings.Index(s, substr) // want "strings.Index can be simplified using strings.Cut" |
| 55 |
if i >= 0 { |
| 56 |
print(s[i+len(substr):]) |
| 57 |
} |
| 58 |
return i >= 0 |
| 59 |
} |
| 60 |
|
| 61 |
func wrong_len_arg(s string, substr string) bool { |
| 62 |
i := strings.Index(s, substr) // don't modernize since i+len(s) is not valid |
| 63 |
print(s[i+len(s):]) |
| 64 |
return i >= 0 |
| 65 |
} |
| 66 |
|
| 67 |
func basic_strings_byte(s string) bool { |
| 68 |
i := strings.IndexByte(s, '+') // want "strings.IndexByte can be simplified using strings.Cut" |
| 69 |
if i >= 0 { |
| 70 |
print(s[:i]) |
| 71 |
} |
| 72 |
return i >= 0 |
| 73 |
} |
| 74 |
|
| 75 |
func basic_strings_byte_int(s string) bool { |
| 76 |
i := strings.IndexByte(s, 55) // want "strings.IndexByte can be simplified using strings.Cut" |
| 77 |
if i >= 0 { |
| 78 |
print(s[:i]) |
| 79 |
} |
| 80 |
return i >= 0 |
| 81 |
} |
| 82 |
|
| 83 |
func basic_strings_byte_var(s string) bool { |
| 84 |
b := byte('b') |
| 85 |
i := strings.IndexByte(s, b) // want "strings.IndexByte can be simplified using strings.Cut" |
| 86 |
if i >= 0 { |
| 87 |
print(s[:i]) |
| 88 |
} |
| 89 |
return i >= 0 |
| 90 |
} |
| 91 |
|
| 92 |
func basic_bytes(b []byte) []byte { |
| 93 |
i := bytes.Index(b, []byte("str")) // want "bytes.Index can be simplified using bytes.Cut" |
| 94 |
if i >= 0 { |
| 95 |
return b[:i] |
| 96 |
} else { |
| 97 |
return b[i+3:] |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
func basic_index_bytes(b []byte) string { |
| 102 |
i := bytes.IndexByte(b, 's') // don't modernize: b[i+1:] in else is not guarded |
| 103 |
if i >= 0 { |
| 104 |
return string(b[:i]) |
| 105 |
} else { |
| 106 |
return string(b[i+1:]) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
func const_substr_len(s string) bool { |
| 111 |
i := strings.Index(s, "=") // want "strings.Index can be simplified using strings.Cut" |
| 112 |
if i >= 0 { |
| 113 |
r := s[i+len("="):] |
| 114 |
return len(r) > 0 |
| 115 |
} |
| 116 |
return false |
| 117 |
} |
| 118 |
|
| 119 |
func const_for_len(s string) bool { |
| 120 |
i := strings.Index(s, "=") // want "strings.Index can be simplified using strings.Cut" |
| 121 |
if i >= 0 { |
| 122 |
r := s[i+1:] |
| 123 |
return len(r) > 0 |
| 124 |
} |
| 125 |
return false |
| 126 |
} |
| 127 |
|
| 128 |
func index(s string) bool { |
| 129 |
i := strings.Index(s, "=") // want "strings.Index can be simplified using strings.Cut" |
| 130 |
if i < 0 { |
| 131 |
return false |
| 132 |
} |
| 133 |
if i >= 0 { |
| 134 |
return true |
| 135 |
} |
| 136 |
print(s[:i]) |
| 137 |
return true |
| 138 |
} |
| 139 |
|
| 140 |
func index_flipped(s string) bool { |
| 141 |
i := strings.Index(s, "=") // want "strings.Index can be simplified using strings.Cut" |
| 142 |
if 0 > i { |
| 143 |
return false |
| 144 |
} |
| 145 |
if 0 <= i { |
| 146 |
return true |
| 147 |
} |
| 148 |
print(s[:i]) |
| 149 |
return true |
| 150 |
} |
| 151 |
|
| 152 |
func invalid_index(s string) bool { |
| 153 |
i := strings.Index(s, "=") // don't modernize since i is used in an "invalid" binaryexpr |
| 154 |
if 0 > i { |
| 155 |
return false |
| 156 |
} |
| 157 |
if i < 10 { |
| 158 |
return true |
| 159 |
} |
| 160 |
return true |
| 161 |
} |
| 162 |
|
| 163 |
func invalid_slice(s string) string { |
| 164 |
i := strings.Index(s, "=") // don't modernize since i is used in an "invalid" slice index |
| 165 |
if i >= 0 { |
| 166 |
return s[i+4:] |
| 167 |
} |
| 168 |
return "" |
| 169 |
} |
| 170 |
|
| 171 |
func index_and_before_after(s string) string { |
| 172 |
substr := "=" |
| 173 |
i := strings.Index(s, substr) // don't modernize: s[i+len(substr):] and s[len(substr)+i:] not nonneg-guarded |
| 174 |
if i == -1 { |
| 175 |
print("test") |
| 176 |
} |
| 177 |
if i < 0 { |
| 178 |
return "" |
| 179 |
} else { |
| 180 |
if i >= 0 { |
| 181 |
return s[:i] |
| 182 |
} else { |
| 183 |
return s[i+len(substr):] |
| 184 |
} |
| 185 |
} |
| 186 |
if -1 == i { |
| 187 |
return s[len(substr)+i:] |
| 188 |
} |
| 189 |
return "final" |
| 190 |
} |
| 191 |
|
| 192 |
func idx_var_init(s string) (string, string) { |
| 193 |
var idx = strings.Index(s, "=") // don't modernize: s[0:idx] is not guarded |
| 194 |
return s[0:idx], s |
| 195 |
} |
| 196 |
|
| 197 |
func idx_reassigned(s string) string { |
| 198 |
idx := strings.Index(s, "=") // don't modernize since idx gets reassigned |
| 199 |
idx = 10 |
| 200 |
return s[:idx] |
| 201 |
} |
| 202 |
|
| 203 |
func idx_printed(s string) string { |
| 204 |
idx := strings.Index(s, "=") // don't modernize since idx is used |
| 205 |
print(idx) |
| 206 |
return s[:idx] |
| 207 |
} |
| 208 |
|
| 209 |
func idx_aliased(s string) string { |
| 210 |
idx := strings.Index(s, "=") // don't modernize since idx gets aliased |
| 211 |
i := idx |
| 212 |
return s[:i] |
| 213 |
} |
| 214 |
|
| 215 |
func idx_aliased_var(s string) string { |
| 216 |
idx := strings.Index(s, "=") // don't modernize since idx gets aliased |
| 217 |
var i = idx |
| 218 |
print(i) |
| 219 |
return s[:idx] |
| 220 |
} |
| 221 |
|
| 222 |
// Regression test for golang/go#78643: |
| 223 |
// when Index is not the sole RHS in an assignment, rewriting to Cut |
| 224 |
// produces invalid Go (`x, before, _, ok := 0, strings.Cut(...)`). |
| 225 |
func multiAssign(s, sub string) int { |
| 226 |
ret, idx := 0, strings.Index(s, sub) // don't modernize: Index is not the sole RHS |
| 227 |
if idx >= 0 { |
| 228 |
prefix := s[:idx] |
| 229 |
_ = prefix |
| 230 |
} |
| 231 |
return ret |
| 232 |
} |
| 233 |
|
| 234 |
func s_modified(s string) string { |
| 235 |
idx := strings.Index(s, "=") // don't modernize since s gets modified |
| 236 |
s = "newstring" |
| 237 |
return s[:idx] |
| 238 |
} |
| 239 |
|
| 240 |
func s_modified_multi_assign(s string) string { |
| 241 |
idx := strings.Index(s, ",") // don't modernize since s gets modified in multi-assignment |
| 242 |
var str string |
| 243 |
str, s = "str", "modified" // modifying use of s |
| 244 |
_ = str |
| 245 |
if idx >= 0 { |
| 246 |
return s[:idx] |
| 247 |
} |
| 248 |
return s |
| 249 |
} |
| 250 |
|
| 251 |
func s_modified_no_params() string { |
| 252 |
s := "string" |
| 253 |
idx := strings.Index(s, "=") // don't modernize since s gets modified |
| 254 |
s = "newstring" |
| 255 |
return s[:idx] |
| 256 |
} |
| 257 |
|
| 258 |
func s_in_func_call() string { |
| 259 |
s := "string" |
| 260 |
substr := "substr" |
| 261 |
idx := strings.Index(s, substr) // don't modernize: s[:idx] is not guarded |
| 262 |
function(s) |
| 263 |
return s[:idx] |
| 264 |
} |
| 265 |
|
| 266 |
func s_pointer() string { |
| 267 |
s := "string" |
| 268 |
idx := strings.Index(s, "s") |
| 269 |
ptr := &s // don't modernize since s may get modified |
| 270 |
reference_str(ptr) |
| 271 |
return s[:idx] |
| 272 |
} |
| 273 |
|
| 274 |
func s_pointer_before_call() string { |
| 275 |
s := "string" |
| 276 |
ptr := &s // don't modernize since s may get modified |
| 277 |
reference_str(ptr) |
| 278 |
idx := strings.Index(s, "s") |
| 279 |
return s[:idx] |
| 280 |
} |
| 281 |
|
| 282 |
func idx_used_before(s string, sub string) string { |
| 283 |
var index int |
| 284 |
reference_int(&index) |
| 285 |
index = strings.Index(s, sub) // don't modernize since index may get modified |
| 286 |
blank() |
| 287 |
if index >= 0 { |
| 288 |
return s[:index] |
| 289 |
} |
| 290 |
return "" |
| 291 |
} |
| 292 |
|
| 293 |
func idx_used_other_substr(s string, sub string) string { |
| 294 |
otherstr := "other" |
| 295 |
i := strings.Index(s, sub) |
| 296 |
print(otherstr[:i]) // don't modernize since i used in another slice expr |
| 297 |
if i >= 0 { |
| 298 |
return s[:i] |
| 299 |
} else { |
| 300 |
return "" |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
func idx_gtr_zero_invalid(s string, sub string) string { |
| 305 |
i := strings.Index(s, sub) |
| 306 |
if i > 0 { // don't modernize since this is a stronger claim than i >= 0 |
| 307 |
return s[:i] |
| 308 |
} |
| 309 |
return "" |
| 310 |
} |
| 311 |
|
| 312 |
func idx_gtreq_one_invalid(s string, sub string) string { |
| 313 |
i := strings.Index(s, sub) |
| 314 |
if i >= 1 { // don't modernize since this is a stronger claim than i >= 0 |
| 315 |
return s[:i] |
| 316 |
} |
| 317 |
return "" |
| 318 |
} |
| 319 |
|
| 320 |
func idx_gtr_negone(s string, sub string) string { |
| 321 |
i := strings.Index(s, sub) // want "strings.Index can be simplified using strings.Cut" |
| 322 |
if i > -1 { |
| 323 |
return s[:i] |
| 324 |
} |
| 325 |
if i != -1 { |
| 326 |
return s |
| 327 |
} |
| 328 |
return "" |
| 329 |
} |
| 330 |
|
| 331 |
// Regression test for a crash (https://go.dev/issue/77208) |
| 332 |
func idx_call() { |
| 333 |
i := bytes.Index(b(), []byte("")) |
| 334 |
_ = i |
| 335 |
} |
| 336 |
|
| 337 |
// Fix for golang/go#77566 |
| 338 |
func multipleCallsSameScope(s string) (bool, bool) { |
| 339 |
i := strings.Index(s, "=") // want "strings.Index can be simplified using strings.Cut" |
| 340 |
if i != -1 { |
| 341 |
print(s[:i]) |
| 342 |
} |
| 343 |
j := strings.Index(s, "-") // want "strings.Index can be simplified using strings.Cut" |
| 344 |
if j != -1 { |
| 345 |
print(s[:j]) |
| 346 |
} |
| 347 |
return i >= 0, j >= 0 |
| 348 |
} |
| 349 |
|
| 350 |
func shadowing(s string) string { |
| 351 |
ok := "true" |
| 352 |
before := "before" |
| 353 |
print(before) // declared within scope but not used after the index call, so no fresh name |
| 354 |
i := strings.Index(s, "=") // want "strings.Index can be simplified using strings.Cut" |
| 355 |
print(ok) |
| 356 |
if i >= 0 { |
| 357 |
print(s[:i]) |
| 358 |
print(i >= 0) |
| 359 |
return s[i+1:] |
| 360 |
} |
| 361 |
return "" |
| 362 |
} |
| 363 |
|
| 364 |
func foreshadowing(s string) string { |
| 365 |
i := strings.Index(s, "=") // want "strings.Index can be simplified using strings.Cut" |
| 366 |
if i != -1 { |
| 367 |
return s[i+1:] |
| 368 |
} |
| 369 |
// Generate a fresh name for ok because it is used within the scope after the Index call. |
| 370 |
ok, m := true, "m" |
| 371 |
if ok { |
| 372 |
return m |
| 373 |
} |
| 374 |
if i != -1 { |
| 375 |
return s |
| 376 |
} |
| 377 |
return "" |
| 378 |
} |
| 379 |
|
| 380 |
func b() []byte { |
| 381 |
return nil |
| 382 |
} |
| 383 |
|
| 384 |
func function(s string) {} |
| 385 |
|
| 386 |
func reference_str(s *string) {} |
| 387 |
|
| 388 |
func reference_int(i *int) {} |
| 389 |
|
| 390 |
func blank() {} |
| 391 |
|
| 392 |
// Regression test for unguarded slice uses (https://go.dev/issue/77737). |
| 393 |
// The s[colon+1:] usage outside the "if" relies on -1+1=0 to return the full |
| 394 |
// string when the separator is absent. Rewriting to "after" would return "". |
| 395 |
func unguarded_after_slice(s string) (int, string) { |
| 396 |
colon := strings.Index(s, ":") |
| 397 |
if colon != -1 { |
| 398 |
print(s[:colon]) |
| 399 |
} |
| 400 |
return colon, s[colon+1:] // don't modernize: s[colon+1:] not guarded |
| 401 |
} |
| 402 |
|
| 403 |
// Same as above but with the guard using i < 0. |
| 404 |
func unguarded_after_slice_negcheck(s string) string { |
| 405 |
i := strings.Index(s, ":") |
| 406 |
if i < 0 { |
| 407 |
print("not found") |
| 408 |
} |
| 409 |
return s[i+1:] // don't modernize: s[i+1:] not guarded |
| 410 |
} |
| 411 |
|
| 412 |
// Safe: both slice uses are inside the nonneg guard. |
| 413 |
func guarded_both_slices(s string) (string, string) { |
| 414 |
i := strings.Index(s, ":") // want "strings.Index can be simplified using strings.Cut" |
| 415 |
if i >= 0 { |
| 416 |
return s[:i], s[i+1:] |
| 417 |
} |
| 418 |
return "", s |
| 419 |
} |
| 420 |
|
| 421 |
// Safe: slice uses after early-return negative check. |
| 422 |
func guarded_early_return(s string) (string, string) { |
| 423 |
i := strings.Index(s, ":") // want "strings.Index can be simplified using strings.Cut" |
| 424 |
if i < 0 { |
| 425 |
return "", s |
| 426 |
} |
| 427 |
return s[:i], s[i+1:] |
| 428 |
} |
| 429 |
|
| 430 |
// Safe: slice uses in else of negative check. |
| 431 |
func guarded_neg_else(s string) (string, string) { |
| 432 |
i := strings.Index(s, ":") // want "strings.Index can be simplified using strings.Cut" |
| 433 |
if i == -1 { |
| 434 |
return "", s |
| 435 |
} else { |
| 436 |
return s[:i], s[i+1:] |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
// -- strings.Split/SplitN [0] → strings.Cut -- |
| 441 |
|
| 442 |
// Should fire: SplitN(s, ",", 2)[0] in short var decl. |
| 443 |
func splitn_zero_define(s string) string { |
| 444 |
x := strings.SplitN(s, ",", 2)[0] // want `strings.SplitN call can be simplified using strings.Cut` |
| 445 |
return x |
| 446 |
} |
| 447 |
|
| 448 |
// Should fire: Split(s, ",")[0] in short var decl. |
| 449 |
func split_zero_define(s string) string { |
| 450 |
x := strings.Split(s, ",")[0] // want `strings.Split call can be simplified using strings.Cut` |
| 451 |
return x |
| 452 |
} |
| 453 |
|
| 454 |
// Should NOT fire: regular assignment (not :=). |
| 455 |
func split_zero_assign(s string) string { |
| 456 |
var x string |
| 457 |
x = strings.Split(s, ",")[0] |
| 458 |
return x |
| 459 |
} |
| 460 |
|
| 461 |
// Should NOT fire: sep is a variable (value unknown at analysis time). |
| 462 |
func split_variable_sep(s, sep string) string { |
| 463 |
x := strings.Split(s, sep)[0] |
| 464 |
return x |
| 465 |
} |
| 466 |
|
| 467 |
// Should NOT fire: sep is the empty string (different semantics from strings.Cut). |
| 468 |
func split_empty_sep(s string) string { |
| 469 |
x := strings.Split(s, "")[0] |
| 470 |
return x |
| 471 |
} |
| 472 |
|
| 473 |
// Should NOT fire: index is 1, not 0. |
| 474 |
func split_index_one(s, sep string) string { |
| 475 |
return strings.Split(s, sep)[1] |
| 476 |
} |
| 477 |
|
| 478 |
// Should NOT fire: SplitN with n != 2. |
| 479 |
func splitn_n3(s, sep string) string { |
| 480 |
return strings.SplitN(s, sep, 3)[0] |
| 481 |
} |
| 482 |
|
| 483 |
// Should NOT fire: result stored in variable first (not inline). |
| 484 |
func split_variable_first(s, sep string) string { |
| 485 |
parts := strings.Split(s, sep) |
| 486 |
return parts[0] |
| 487 |
} |
| 488 |
|
| 489 |
// Should NOT fire: LHS is blank identifier. |
| 490 |
func split_blank_lhs(s, sep string) { |
| 491 |
_ = strings.Split(s, sep)[0] |
| 492 |
} |
| 493 |
|
| 494 |
// Should NOT fire: multiple values on LHS. |
| 495 |
func split_multi_lhs(s, sep string) (string, string) { |
| 496 |
x, y := strings.Split(s, sep)[0], strings.Split(s, sep)[0] |
| 497 |
return x, y |
| 498 |
} |