modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/loopbce.go (about)

     1  // +build amd64
     2  // errorcheck -0 -d=ssa/loopbce/debug=3
     3  
     4  package main
     5  
     6  func f0a(a []int) int {
     7  	x := 0
     8  	for i := range a { // ERROR "Induction variable with minimum 0 and increment 1$"
     9  		x += a[i] // ERROR "Found redundant IsInBounds$"
    10  	}
    11  	return x
    12  }
    13  
    14  func f0b(a []int) int {
    15  	x := 0
    16  	for i := range a { // ERROR "Induction variable with minimum 0 and increment 1$"
    17  		b := a[i:] // ERROR "Found redundant IsSliceInBounds$"
    18  		x += b[0]
    19  	}
    20  	return x
    21  }
    22  
    23  func f0c(a []int) int {
    24  	x := 0
    25  	for i := range a { // ERROR "Induction variable with minimum 0 and increment 1$"
    26  		b := a[:i+1] // ERROR "Found redundant IsSliceInBounds \(len promoted to cap\)$"
    27  		x += b[0]
    28  	}
    29  	return x
    30  }
    31  
    32  func f1(a []int) int {
    33  	x := 0
    34  	for _, i := range a { // ERROR "Induction variable with minimum 0 and increment 1"
    35  		x += i
    36  	}
    37  	return x
    38  }
    39  
    40  func f2(a []int) int {
    41  	x := 0
    42  	for i := 1; i < len(a); i++ { // ERROR "Induction variable with minimum 1 and increment 1$"
    43  		x += a[i] // ERROR "Found redundant IsInBounds$"
    44  	}
    45  	return x
    46  }
    47  
    48  func f4(a [10]int) int {
    49  	x := 0
    50  	for i := 0; i < len(a); i += 2 { // ERROR "Induction variable with minimum 0 and increment 2$"
    51  		x += a[i] // ERROR "Found redundant IsInBounds$"
    52  	}
    53  	return x
    54  }
    55  
    56  func f5(a [10]int) int {
    57  	x := 0
    58  	for i := -10; i < len(a); i += 2 { // ERROR "Induction variable with minimum -10 and increment 2$"
    59  		x += a[i]
    60  	}
    61  	return x
    62  }
    63  
    64  func f6(a []int) {
    65  	for i := range a { // ERROR "Induction variable with minimum 0 and increment 1$"
    66  		b := a[0:i] // ERROR "Found redundant IsSliceInBounds \(len promoted to cap\)$"
    67  		f6(b)
    68  	}
    69  }
    70  
    71  func g0a(a string) int {
    72  	x := 0
    73  	for i := 0; i < len(a); i++ { // ERROR "Induction variable with minimum 0 and increment 1$"
    74  		x += int(a[i]) // ERROR "Found redundant IsInBounds$"
    75  	}
    76  	return x
    77  }
    78  
    79  func g0b(a string) int {
    80  	x := 0
    81  	for i := 0; len(a) > i; i++ { // ERROR "Induction variable with minimum 0 and increment 1$"
    82  		x += int(a[i]) // ERROR "Found redundant IsInBounds$"
    83  	}
    84  	return x
    85  }
    86  
    87  func g1() int {
    88  	a := "evenlength"
    89  	x := 0
    90  	for i := 0; i < len(a); i += 2 { // ERROR "Induction variable with minimum 0 and increment 2$"
    91  		x += int(a[i]) // ERROR "Found redundant IsInBounds$"
    92  	}
    93  	return x
    94  }
    95  
    96  func g2() int {
    97  	a := "evenlength"
    98  	x := 0
    99  	for i := 0; i < len(a); i += 2 { // ERROR "Induction variable with minimum 0 and increment 2$"
   100  		j := i
   101  		if a[i] == 'e' { // ERROR "Found redundant IsInBounds$"
   102  			j = j + 1
   103  		}
   104  		x += int(a[j])
   105  	}
   106  	return x
   107  }
   108  
   109  func g3a() {
   110  	a := "this string has length 25"
   111  	for i := 0; i < len(a); i += 5 { // ERROR "Induction variable with minimum 0 and increment 5$"
   112  		useString(a[i:]) // ERROR "Found redundant IsSliceInBounds$"
   113  		useString(a[:i+3])
   114  	}
   115  }
   116  
   117  func g3b(a string) {
   118  	for i := 0; i < len(a); i++ { // ERROR "Induction variable with minimum 0 and increment 1$"
   119  		useString(a[i+1:]) // ERROR "Found redundant IsSliceInBounds$"
   120  	}
   121  }
   122  
   123  func g3c(a string) {
   124  	for i := 0; i < len(a); i++ { // ERROR "Induction variable with minimum 0 and increment 1$"
   125  		useString(a[:i+1]) // ERROR "Found redundant IsSliceInBounds$"
   126  	}
   127  }
   128  
   129  func h1(a []byte) {
   130  	c := a[:128]
   131  	for i := range c { // ERROR "Induction variable with minimum 0 and increment 1$"
   132  		c[i] = byte(i) // ERROR "Found redundant IsInBounds$"
   133  	}
   134  }
   135  
   136  func h2(a []byte) {
   137  	for i := range a[:128] { // ERROR "Induction variable with minimum 0 and increment 1$"
   138  		a[i] = byte(i)
   139  	}
   140  }
   141  
   142  func k0(a [100]int) [100]int {
   143  	for i := 10; i < 90; i++ { // ERROR "Induction variable with minimum 10 and increment 1$"
   144  		a[i-11] = i
   145  		a[i-10] = i // ERROR "Found redundant \(IsInBounds ind 100\), ind < 80$"
   146  		a[i-5] = i  // ERROR "Found redundant \(IsInBounds ind 100\), ind < 85$"
   147  		a[i] = i    // ERROR "Found redundant \(IsInBounds ind 100\), ind < 90$"
   148  		a[i+5] = i  // ERROR "Found redundant \(IsInBounds ind 100\), ind < 95$"
   149  		a[i+10] = i // ERROR "Found redundant \(IsInBounds ind 100\), ind < 100$"
   150  		a[i+11] = i
   151  	}
   152  	return a
   153  }
   154  
   155  func k1(a [100]int) [100]int {
   156  	for i := 10; i < 90; i++ { // ERROR "Induction variable with minimum 10 and increment 1$"
   157  		useSlice(a[:i-11])
   158  		useSlice(a[:i-10]) // ERROR "Found redundant \(IsSliceInBounds ind 100\), ind < 80$"
   159  		useSlice(a[:i-5])  // ERROR "Found redundant \(IsSliceInBounds ind 100\), ind < 85$"
   160  		useSlice(a[:i])    // ERROR "Found redundant \(IsSliceInBounds ind 100\), ind < 90$"
   161  		useSlice(a[:i+5])  // ERROR "Found redundant \(IsSliceInBounds ind 100\), ind < 95$"
   162  		useSlice(a[:i+10]) // ERROR "Found redundant \(IsSliceInBounds ind 100\), ind < 100$"
   163  		useSlice(a[:i+11]) // ERROR "Found redundant \(IsSliceInBounds ind 100\), ind < 101$"
   164  
   165  	}
   166  	return a
   167  }
   168  
   169  func k2(a [100]int) [100]int {
   170  	for i := 10; i < 90; i++ { // ERROR "Induction variable with minimum 10 and increment 1$"
   171  		useSlice(a[i-11:])
   172  		useSlice(a[i-10:]) // ERROR "Found redundant \(IsSliceInBounds ind 100\), ind < 80$"
   173  		useSlice(a[i-5:])  // ERROR "Found redundant \(IsSliceInBounds ind 100\), ind < 85$"
   174  		useSlice(a[i:])    // ERROR "Found redundant \(IsSliceInBounds ind 100\), ind < 90$"
   175  		useSlice(a[i+5:])  // ERROR "Found redundant \(IsSliceInBounds ind 100\), ind < 95$"
   176  		useSlice(a[i+10:]) // ERROR "Found redundant \(IsSliceInBounds ind 100\), ind < 100$"
   177  		useSlice(a[i+11:]) // ERROR "Found redundant \(IsSliceInBounds ind 100\), ind < 101$"
   178  	}
   179  	return a
   180  }
   181  
   182  func k3(a [100]int) [100]int {
   183  	for i := -10; i < 90; i++ { // ERROR "Induction variable with minimum -10 and increment 1$"
   184  		a[i+10] = i // ERROR "Found redundant \(IsInBounds ind 100\), ind < 100$"
   185  	}
   186  	return a
   187  }
   188  
   189  func k4(a [100]int) [100]int {
   190  	min := (-1) << 63
   191  	for i := min; i < min+50; i++ { // ERROR "Induction variable with minimum -9223372036854775808 and increment 1$"
   192  		a[i-min] = i // ERROR "Found redundant \(IsInBounds ind 100\), ind < 50$"
   193  	}
   194  	return a
   195  }
   196  
   197  func k5(a [100]int) [100]int {
   198  	max := (1 << 63) - 1
   199  	for i := max - 50; i < max; i++ { // ERROR "Induction variable with minimum 9223372036854775757 and increment 1$"
   200  		a[i-max+50] = i   // ERROR "Found redundant \(IsInBounds ind 100\), ind < 50$"
   201  		a[i-(max-70)] = i // ERROR "Found redundant \(IsInBounds ind 100\), ind < 70$"
   202  	}
   203  	return a
   204  }
   205  
   206  func nobce1() {
   207  	// tests overflow of max-min
   208  	a := int64(9223372036854774057)
   209  	b := int64(-1547)
   210  	z := int64(1337)
   211  
   212  	if a%z == b%z {
   213  		panic("invalid test: modulos should differ")
   214  	}
   215  
   216  	for i := b; i < a; i += z {
   217  		// No induction variable is possible because i will overflow a first iteration.
   218  		useString("foobar")
   219  	}
   220  }
   221  
   222  func nobce2(a string) {
   223  	for i := int64(0); i < int64(len(a)); i++ { // ERROR "Induction variable with minimum 0 and increment 1$"
   224  		useString(a[i:]) // ERROR "Found redundant IsSliceInBounds$"
   225  	}
   226  	for i := int64(0); i < int64(len(a))-31337; i++ { // ERROR "Induction variable with minimum 0 and increment 1$"
   227  		useString(a[i:]) // ERROR "Found redundant IsSliceInBounds$"
   228  	}
   229  	for i := int64(0); i < int64(len(a))+int64(-1<<63); i++ { // ERROR "Induction variable with minimum 0 and increment 1$"
   230  		// tests an overflow of StringLen-MinInt64
   231  		useString(a[i:])
   232  	}
   233  }
   234  
   235  func nobce3(a [100]int64) [100]int64 {
   236  	min := int64((-1) << 63)
   237  	max := int64((1 << 63) - 1)
   238  	for i := min; i < max; i++ { // ERROR "Induction variable with minimum -9223372036854775808 and increment 1$"
   239  		a[i] = i
   240  	}
   241  	return a
   242  }
   243  
   244  //go:noinline
   245  func useString(a string) {
   246  }
   247  
   248  //go:noinline
   249  func useSlice(a []int) {
   250  }
   251  
   252  func main() {
   253  }