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

     1  // errorcheck
     2  
     3  // Copyright 2013 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package p
     8  
     9  var array *[10]int
    10  var slice []int
    11  var str string
    12  var i, j, k int
    13  
    14  func f() {
    15  	// check what missing arguments are allowed
    16  	_ = array[:]
    17  	_ = array[i:]
    18  	_ = array[:j]
    19  	_ = array[i:j]
    20  	_ = array[::] // ERROR "middle index required in 3-index slice" "final index required in 3-index slice"
    21  	_ = array[i::] // ERROR "middle index required in 3-index slice" "final index required in 3-index slice"
    22  	_ = array[:j:] // ERROR "final index required in 3-index slice"
    23  	_ = array[i:j:] // ERROR "final index required in 3-index slice"
    24  	_ = array[::k] // ERROR "middle index required in 3-index slice"
    25  	_ = array[i::k] // ERROR "middle index required in 3-index slice"
    26  	_ = array[:j:k]
    27  	_ = array[i:j:k]
    28  	
    29  	_ = slice[:]
    30  	_ = slice[i:]
    31  	_ = slice[:j]
    32  	_ = slice[i:j]
    33  	_ = slice[::] // ERROR "middle index required in 3-index slice" "final index required in 3-index slice"
    34  	_ = slice[i::] // ERROR "middle index required in 3-index slice" "final index required in 3-index slice"
    35  	_ = slice[:j:] // ERROR "final index required in 3-index slice"
    36  	_ = slice[i:j:] // ERROR "final index required in 3-index slice"
    37  	_ = slice[::k] // ERROR "middle index required in 3-index slice"
    38  	_ = slice[i::k] // ERROR "middle index required in 3-index slice"
    39  	_ = slice[:j:k]
    40  	_ = slice[i:j:k]
    41  	
    42  	_ = str[:]
    43  	_ = str[i:]
    44  	_ = str[:j]
    45  	_ = str[i:j]
    46  	_ = str[::] // ERROR "3-index slice of string" "middle index required in 3-index slice" "final index required in 3-index slice"
    47  	_ = str[i::] // ERROR "3-index slice of string" "middle index required in 3-index slice" "final index required in 3-index slice"
    48  	_ = str[:j:] // ERROR "3-index slice of string" "final index required in 3-index slice"
    49  	_ = str[i:j:] // ERROR "3-index slice of string" "final index required in 3-index slice"
    50  	_ = str[::k] // ERROR "3-index slice of string" "middle index required in 3-index slice"
    51  	_ = str[i::k] // ERROR "3-index slice of string" "middle index required in 3-index slice"
    52  	_ = str[:j:k] // ERROR "3-index slice of string"
    53  	_ = str[i:j:k] // ERROR "3-index slice of string"
    54  
    55  	// check invalid indices
    56  	_ = array[1:2]
    57  	_ = array[2:1] // ERROR "invalid slice index|inverted slice"
    58  	_ = array[2:2]
    59  	_ = array[i:1]
    60  	_ = array[1:j]
    61  	_ = array[1:2:3]
    62  	_ = array[1:3:2] // ERROR "invalid slice index|inverted slice"
    63  	_ = array[2:1:3] // ERROR "invalid slice index|inverted slice"
    64  	_ = array[2:3:1] // ERROR "invalid slice index|inverted slice"
    65  	_ = array[3:1:2] // ERROR "invalid slice index|inverted slice"
    66  	_ = array[3:2:1] // ERROR "invalid slice index|inverted slice"
    67  	_ = array[i:1:2]
    68  	_ = array[i:2:1] // ERROR "invalid slice index|inverted slice"
    69  	_ = array[1:j:2]
    70  	_ = array[2:j:1] // ERROR "invalid slice index"
    71  	_ = array[1:2:k]
    72  	_ = array[2:1:k] // ERROR "invalid slice index|inverted slice"
    73  	
    74  	_ = slice[1:2]
    75  	_ = slice[2:1] // ERROR "invalid slice index|inverted slice"
    76  	_ = slice[2:2]
    77  	_ = slice[i:1]
    78  	_ = slice[1:j]
    79  	_ = slice[1:2:3]
    80  	_ = slice[1:3:2] // ERROR "invalid slice index|inverted slice"
    81  	_ = slice[2:1:3] // ERROR "invalid slice index|inverted slice"
    82  	_ = slice[2:3:1] // ERROR "invalid slice index|inverted slice"
    83  	_ = slice[3:1:2] // ERROR "invalid slice index|inverted slice"
    84  	_ = slice[3:2:1] // ERROR "invalid slice index|inverted slice"
    85  	_ = slice[i:1:2]
    86  	_ = slice[i:2:1] // ERROR "invalid slice index|inverted slice"
    87  	_ = slice[1:j:2]
    88  	_ = slice[2:j:1] // ERROR "invalid slice index"
    89  	_ = slice[1:2:k]
    90  	_ = slice[2:1:k] // ERROR "invalid slice index|inverted slice"
    91  	
    92  	_ = str[1:2]
    93  	_ = str[2:1] // ERROR "invalid slice index|inverted slice"
    94  	_ = str[2:2]
    95  	_ = str[i:1]
    96  	_ = str[1:j]
    97  
    98  	// check out of bounds indices on array
    99  	_ = array[11:11] // ERROR "out of bounds"
   100  	_ = array[11:12] // ERROR "out of bounds"
   101  	_ = array[11:] // ERROR "out of bounds"
   102  	_ = array[:11] // ERROR "out of bounds"
   103  	_ = array[1:11] // ERROR "out of bounds"
   104  	_ = array[1:11:12] // ERROR "out of bounds"
   105  	_ = array[1:2:11] // ERROR "out of bounds"
   106  	_ = array[1:11:3] // ERROR "out of bounds|invalid slice index"
   107  	_ = array[11:2:3] // ERROR "out of bounds|inverted slice|invalid slice index"
   108  	_ = array[11:12:13] // ERROR "out of bounds"
   109  
   110  	// slice bounds not checked
   111  	_ = slice[11:11]
   112  	_ = slice[11:12]
   113  	_ = slice[11:]
   114  	_ = slice[:11]
   115  	_ = slice[1:11]
   116  	_ = slice[1:11:12]
   117  	_ = slice[1:2:11]
   118  	_ = slice[1:11:3] // ERROR "invalid slice index"
   119  	_ = slice[11:2:3] // ERROR "invalid slice index|inverted slice"
   120  	_ = slice[11:12:13]
   121  }