github.com/amarpal/go-tools@v0.0.0-20240422043104-40142f59f616/simple/s1017/testdata/src/example.com/CheckTrim/trim.go (about) 1 package pkg 2 3 import ( 4 "bytes" 5 "strings" 6 ) 7 8 func foo(s string) int { return 0 } 9 func gen() string { 10 return "" 11 } 12 13 func fn() { 14 const s1 = "a string value" 15 var s2 = "a string value" 16 const n = 14 17 18 var id1 = "a string value" 19 var id2 string 20 if strings.HasPrefix(id1, s1) { //@ diag(re`should replace.*with.*strings\.TrimPrefix`) 21 id1 = id1[len(s1):] 22 } 23 24 if strings.HasPrefix(id1, s1) { //@ diag(re`should replace.*with.*strings\.TrimPrefix`) 25 id1 = strings.TrimPrefix(id1, s1) 26 } 27 28 if strings.HasPrefix(id1, s1) { 29 id1 = strings.TrimPrefix(id1, s2) 30 } 31 32 if strings.Contains(id1, s1) { //@ diag(re`should replace.*with.*strings\.Replace`) 33 id1 = strings.Replace(id1, s1, "something", 123) 34 } 35 36 if strings.HasSuffix(id1, s2) { //@ diag(re`should replace.*with.*strings\.TrimSuffix`) 37 id1 = id1[:len(id1)-len(s2)] 38 } 39 40 var x, y []string 41 var i int 42 if strings.HasPrefix(x[i], s1) { //@ diag(re`should replace.*with.*strings\.TrimPrefix`) 43 x[i] = x[i][len(s1):] 44 } 45 46 if strings.HasPrefix(x[i], y[i]) { //@ diag(re`should replace.*with.*strings\.TrimPrefix`) 47 x[i] = x[i][len(y[i]):] 48 } 49 50 var t struct{ x string } 51 if strings.HasPrefix(t.x, s1) { //@ diag(re`should replace.*with.*strings\.TrimPrefix`) 52 t.x = t.x[len(s1):] 53 } 54 55 if strings.HasPrefix(id1, "test") { //@ diag(re`should replace.*with.*strings\.TrimPrefix`) 56 id1 = id1[len("test"):] 57 } 58 59 if strings.HasPrefix(id1, "test") { //@ diag(re`should replace.*with.*strings\.TrimPrefix`) 60 id1 = id1[4:] 61 } 62 63 if strings.HasPrefix(id1, s1) { // not allowed, 14 and s1 aren't obviously connected 64 id1 = id1[14:] 65 } 66 67 if strings.HasPrefix(id1, s1) { // not allowed, s1 and n aren't obviously connected 68 id1 = id1[n:] 69 } 70 71 var b1, b2 []byte 72 if bytes.HasPrefix(b1, b2) { //@ diag(re`should replace.*with.*bytes\.TrimPrefix`) 73 b1 = b1[len(b2):] 74 } 75 76 id3 := s2 77 if strings.HasPrefix(id1, id3) { //@ diag(re`should replace.*with.*strings\.TrimPrefix`) 78 id1 = id1[len(id3):] 79 } 80 81 if strings.HasSuffix(id1, s2) { 82 id1 = id1[:len(id1)+len(s2)] // wrong operator 83 } 84 85 if strings.HasSuffix(id1, s2) { 86 id1 = id1[:len(s2)-len(id1)] // wrong math 87 } 88 89 if strings.HasSuffix(id1, s2) { 90 id1 = id1[:len(id1)-len(id1)] // wrong string length 91 } 92 93 if strings.HasPrefix(id1, gen()) { 94 id1 = id1[len(gen()):] // dynamic id3 95 } 96 97 if strings.HasPrefix(id1, s1) { 98 id1 = id1[foo(s1):] // wrong function 99 } 100 101 if strings.HasPrefix(id1, s1) { 102 id1 = id1[len(id1):] // len() on wrong value 103 } 104 105 if strings.HasPrefix(id1, "test") { 106 id1 = id1[5:] // wrong length 107 } 108 109 if strings.HasPrefix(id1, s1) { 110 id1 = id1[len(s1)+1:] // wrong length due to math 111 } 112 113 if strings.HasPrefix(id1, s1) { 114 id2 = id1[len(s1):] // assigning to the wrong variable 115 } 116 117 if strings.HasPrefix(id1, s1) { 118 id1 = id1[len(s1):15] // has a max 119 } 120 121 if strings.HasPrefix(id1, s1) { 122 id1 = id2[len(s1):] // assigning the wrong value 123 } 124 125 if strings.HasPrefix(id1, s1) { 126 id1 = id1[len(s1):] 127 id1 += "" // doing more work in the if 128 } 129 130 if strings.HasPrefix(id1, s1) { 131 id1 = id1[len(s1):] 132 } else { 133 id1 = "game over" // else branch 134 } 135 136 if strings.HasPrefix(id1, s1) { 137 // the conditional is guarding additional code 138 id1 = id1[len(s1):] 139 println(id1) 140 } 141 142 if strings.Contains(id1, s1) { 143 id1 = id1[:] 144 } 145 } 146 147 func fn2() { 148 var s string 149 const id = ".json" 150 151 if strings.HasSuffix(s, ".json") { //@ diag(re`should replace.*with.*strings\.TrimSuffix`) 152 s = strings.TrimSuffix(s, ".json") 153 } 154 155 if strings.HasSuffix(s, ".json") { //@ diag(re`should replace.*with.*strings\.TrimSuffix`) 156 s = s[:len(s)-len(".json")] 157 } 158 159 if strings.HasSuffix(s, ".json") { //@ diag(re`should replace.*with.*strings\.TrimSuffix`) 160 s = s[:len(s)-5] 161 } 162 163 if strings.HasSuffix(s, id) { 164 s = s[:len(s)-5] // second argument of HasSuffix it not a string literal 165 } 166 167 if strings.HasSuffix(s, ".json") { 168 s = s[:len(s)-4] // wrong length 169 } 170 } 171 172 func fn3() { 173 const s1 = "a string value" 174 175 var id1 = "a string value" 176 len := func(string) int { return 0 } // don't accept non-builtin definition of len 177 if strings.HasPrefix(id1, s1) { 178 id1 = id1[len(s1):] 179 } 180 181 if strings.HasSuffix(id1, s1) { 182 id1 = id1[:len(id1)-len(s1)] 183 } 184 }