| 1 |
package stringscutprefix |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
) |
| 6 |
|
| 7 |
var ( |
| 8 |
s, pre, suf string |
| 9 |
) |
| 10 |
|
| 11 |
// test supported cases of pattern 1 - CutPrefix |
| 12 |
func _() { |
| 13 |
if strings.HasPrefix(s, pre) { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" |
| 14 |
a := strings.TrimPrefix(s, pre) |
| 15 |
_ = a |
| 16 |
} |
| 17 |
if strings.HasPrefix("", "") { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" |
| 18 |
a := strings.TrimPrefix("", "") |
| 19 |
_ = a |
| 20 |
} |
| 21 |
if strings.HasPrefix(s, "") { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" |
| 22 |
println([]byte(strings.TrimPrefix(s, ""))) |
| 23 |
} |
| 24 |
if strings.HasPrefix(s, "") { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" |
| 25 |
a, b := "", strings.TrimPrefix(s, "") |
| 26 |
_, _ = a, b |
| 27 |
} |
| 28 |
if strings.HasPrefix(s, "") { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" |
| 29 |
a, b := strings.TrimPrefix(s, ""), strings.TrimPrefix(s, "") // only replace the first occurrence |
| 30 |
s = "123" |
| 31 |
b = strings.TrimPrefix(s, "") // only replace the first occurrence |
| 32 |
_, _ = a, b |
| 33 |
} |
| 34 |
|
| 35 |
var a, b string |
| 36 |
if strings.HasPrefix(s, "") { // want "HasPrefix \\+ TrimPrefix can be simplified to CutPrefix" |
| 37 |
a, b = "", strings.TrimPrefix(s, "") |
| 38 |
_, _ = a, b |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
// test basic cases for CutSuffix - only covering the key differences |
| 43 |
func _() { |
| 44 |
if strings.HasSuffix(s, suf) { // want "HasSuffix \\+ TrimSuffix can be simplified to CutSuffix" |
| 45 |
a := strings.TrimSuffix(s, suf) |
| 46 |
_ = a |
| 47 |
} |
| 48 |
if strings.HasSuffix(s, "") { // want "HasSuffix \\+ TrimSuffix can be simplified to CutSuffix" |
| 49 |
println([]byte(strings.TrimSuffix(s, ""))) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// test cases that are not supported by pattern1 - CutPrefix |
| 54 |
func _() { |
| 55 |
ok := strings.HasPrefix("", "") |
| 56 |
if ok { // noop, currently it doesn't track the result usage of HasPrefix |
| 57 |
a := strings.TrimPrefix("", "") |
| 58 |
_ = a |
| 59 |
} |
| 60 |
if strings.HasPrefix(s, pre) { |
| 61 |
a := strings.TrimPrefix("", "") // noop, as the argument isn't the same |
| 62 |
_ = a |
| 63 |
} |
| 64 |
if strings.HasPrefix(s, pre) { |
| 65 |
var result string |
| 66 |
result = strings.TrimPrefix("", "") // noop, as we believe define is more popular. |
| 67 |
_ = result |
| 68 |
} |
| 69 |
if strings.HasPrefix("", "") { |
| 70 |
a := strings.TrimPrefix(s, pre) // noop, as the argument isn't the same |
| 71 |
_ = a |
| 72 |
} |
| 73 |
if s1 := s; strings.HasPrefix(s1, pre) { |
| 74 |
a := strings.TrimPrefix(s1, pre) // noop, as IfStmt.Init is present |
| 75 |
_ = a |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
// test basic unsupported case for CutSuffix |
| 80 |
func _() { |
| 81 |
if strings.HasSuffix(s, suf) { |
| 82 |
a := strings.TrimSuffix("", "") // noop, as the argument isn't the same |
| 83 |
_ = a |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
var value0 string |
| 88 |
|
| 89 |
// test supported cases of pattern2 - CutPrefix |
| 90 |
func _() { |
| 91 |
if after := strings.TrimPrefix(s, pre); after != s { // want "TrimPrefix can be simplified to CutPrefix" |
| 92 |
println(after) |
| 93 |
} |
| 94 |
if after := strings.TrimPrefix(s, pre); s != after { // want "TrimPrefix can be simplified to CutPrefix" |
| 95 |
println(after) |
| 96 |
} |
| 97 |
if after := strings.TrimPrefix(s, pre); s != after { // want "TrimPrefix can be simplified to CutPrefix" |
| 98 |
println(strings.TrimPrefix(s, pre)) // noop here |
| 99 |
} |
| 100 |
if after := strings.TrimPrefix(s, ""); s != after { // want "TrimPrefix can be simplified to CutPrefix" |
| 101 |
println(after) |
| 102 |
} |
| 103 |
var ok bool // define an ok variable to test the fix won't shadow it for its if stmt body |
| 104 |
if after := strings.TrimPrefix(s, pre); after != s { // want "TrimPrefix can be simplified to CutPrefix" |
| 105 |
_ = ok |
| 106 |
println(after) |
| 107 |
} |
| 108 |
_ = ok // fine to shadow, since ok is not used within the ifstmt block |
| 109 |
if after := strings.TrimPrefix(s, pre); after != s { // want "TrimPrefix can be simplified to CutPrefix" |
| 110 |
println(after) |
| 111 |
} |
| 112 |
var predefined string |
| 113 |
if predefined = strings.TrimPrefix(s, pre); s != predefined { // noop |
| 114 |
println(predefined) |
| 115 |
} |
| 116 |
if predefined = strings.TrimPrefix(s, pre); s != predefined { // noop |
| 117 |
println(&predefined) |
| 118 |
} |
| 119 |
var value string |
| 120 |
if value = strings.TrimPrefix(s, pre); s != value { // noop |
| 121 |
println(value) |
| 122 |
} |
| 123 |
lhsMap := make(map[string]string) |
| 124 |
if lhsMap[""] = strings.TrimPrefix(s, pre); s != lhsMap[""] { // noop |
| 125 |
println(lhsMap[""]) |
| 126 |
} |
| 127 |
arr := make([]string, 0) |
| 128 |
if arr[0] = strings.TrimPrefix(s, pre); s != arr[0] { // noop |
| 129 |
println(arr[0]) |
| 130 |
} |
| 131 |
type example struct { |
| 132 |
field string |
| 133 |
} |
| 134 |
var e example |
| 135 |
if e.field = strings.TrimPrefix(s, pre); s != e.field { // noop |
| 136 |
println(e.field) |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
// test basic cases for pattern2 - CutSuffix |
| 141 |
func _() { |
| 142 |
if before := strings.TrimSuffix(s, suf); before != s { // want "TrimSuffix can be simplified to CutSuffix" |
| 143 |
println(before) |
| 144 |
} |
| 145 |
if before := strings.TrimSuffix(s, suf); s != before { // want "TrimSuffix can be simplified to CutSuffix" |
| 146 |
println(before) |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
// test cases that not supported by pattern2 - CutPrefix |
| 151 |
func _() { |
| 152 |
if after := strings.TrimPrefix(s, pre); s != pre { // noop |
| 153 |
println(after) |
| 154 |
} |
| 155 |
if after := strings.TrimPrefix(s, pre); after != pre { // noop |
| 156 |
println(after) |
| 157 |
} |
| 158 |
if strings.TrimPrefix(s, pre) != s { |
| 159 |
println(strings.TrimPrefix(s, pre)) |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
// test basic unsupported case for pattern2 - CutSuffix |
| 164 |
func _() { |
| 165 |
if before := strings.TrimSuffix(s, suf); s != suf { // noop |
| 166 |
println(before) |
| 167 |
} |
| 168 |
} |