github.com/golangci/go-tools@v0.0.0-20190318060251-af6baa5dc196/simple/testdata/src/trim/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) { // MATCH /should replace.*with.*strings.TrimPrefix/
    21  		id1 = id1[len(s1):]
    22  	}
    23  
    24  	if strings.HasSuffix(id1, s2) { // MATCH /should replace.*with.*strings.TrimSuffix/
    25  		id1 = id1[:len(id1)-len(s2)]
    26  	}
    27  
    28  	var x, y []string
    29  	var i int
    30  	if strings.HasPrefix(x[i], s1) { // MATCH /should replace.*with.*strings.TrimPrefix/
    31  		x[i] = x[i][len(s1):]
    32  	}
    33  
    34  	if strings.HasPrefix(x[i], y[i]) { // MATCH /should replace.*with.*strings.TrimPrefix/
    35  		x[i] = x[i][len(y[i]):]
    36  	}
    37  
    38  	var t struct{ x string }
    39  	if strings.HasPrefix(t.x, s1) { // MATCH /should replace.*with.*strings.TrimPrefix/
    40  		t.x = t.x[len(s1):]
    41  	}
    42  
    43  	if strings.HasPrefix(id1, "test") { // MATCH /should replace.*with.*strings.TrimPrefix/
    44  		id1 = id1[len("test"):]
    45  	}
    46  
    47  	if strings.HasPrefix(id1, "test") { // MATCH /should replace.*with.*strings.TrimPrefix/
    48  		id1 = id1[4:]
    49  	}
    50  
    51  	if strings.HasPrefix(id1, s1) { // MATCH /should replace.*with.*strings.TrimPrefix/
    52  		id1 = id1[14:]
    53  	}
    54  
    55  	if strings.HasPrefix(id1, s1) { // MATCH /should replace.*with.*strings.TrimPrefix/
    56  		id1 = id1[n:]
    57  	}
    58  
    59  	var b1, b2 []byte
    60  	if bytes.HasPrefix(b1, b2) { // MATCH /should replace.*with.*bytes.TrimPrefix/
    61  		b1 = b1[len(b2):]
    62  	}
    63  
    64  	id3 := s2
    65  	if strings.HasPrefix(id1, id3) { // MATCH /should replace.*with.*strings.TrimPrefix/
    66  		id1 = id1[len(id3):]
    67  	}
    68  
    69  	if strings.HasSuffix(id1, s2) {
    70  		id1 = id1[:len(id1)+len(s2)] // wrong operator
    71  	}
    72  
    73  	if strings.HasSuffix(id1, s2) {
    74  		id1 = id1[:len(s2)-len(id1)] // wrong math
    75  	}
    76  
    77  	if strings.HasSuffix(id1, s2) {
    78  		id1 = id1[:len(id1)-len(id1)] // wrong string length
    79  	}
    80  
    81  	if strings.HasPrefix(id1, gen()) {
    82  		id1 = id1[len(gen()):] // dunamic id3
    83  	}
    84  
    85  	if strings.HasPrefix(id1, s1) {
    86  		id1 = id1[foo(s1):] // wrong function
    87  	}
    88  
    89  	if strings.HasPrefix(id1, s1) {
    90  		id1 = id1[len(id1):] // len() on wrong value
    91  	}
    92  
    93  	if strings.HasPrefix(id1, "test") {
    94  		id1 = id1[5:] // wrong length
    95  	}
    96  
    97  	if strings.HasPrefix(id1, s1) {
    98  		id1 = id1[len(s1)+1:] // wrong length due to math
    99  	}
   100  
   101  	if strings.HasPrefix(id1, s1) {
   102  		id2 = id1[len(s1):] // assigning to the wrong variable
   103  	}
   104  
   105  	if strings.HasPrefix(id1, s1) {
   106  		id1 = id1[len(s1):15] // has a max
   107  	}
   108  
   109  	if strings.HasPrefix(id1, s1) {
   110  		id1 = id2[len(s1):] // assigning the wrong value
   111  	}
   112  
   113  	if strings.HasPrefix(id1, s1) {
   114  		id1 = id1[len(s1):]
   115  		id1 += "" // doing more work in the if
   116  	}
   117  
   118  	if strings.HasPrefix(id1, s1) {
   119  		id1 = id1[len(s1):]
   120  	} else {
   121  		id1 = "game over" // else branch
   122  	}
   123  
   124  	if strings.HasPrefix(id1, s1) {
   125  		// the conditional is guarding additional code
   126  		id1 = id1[len(s1):]
   127  		println(id1)
   128  	}
   129  }