1 // errorcheck -0 -d=append,slice 2 3 // Copyright 2015 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 // Check optimization results for append and slicing. 8 9 package main 10 11 func a1(x []int, y int) []int { 12 x = append(x, y) // ERROR "append: len-only update" 13 return x 14 } 15 16 func a2(x []int, y int) []int { 17 return append(x, y) // ERROR "append: full update" 18 } 19 20 func a3(x *[]int, y int) { 21 *x = append(*x, y) // ERROR "append: len-only update" 22 } 23 24 func s1(x **[]int, xs **string, i, j int) { 25 var z []int 26 z = (**x)[2:] // ERROR "slice: omit check for 2nd index" 27 z = (**x)[2:len(**x)] // not yet: "slice: reuse len" "slice: omit check for 2nd index" 28 z = (**x)[2:cap(**x)] // not yet: "slice: reuse cap" "slice: omit check for 2nd index" 29 z = (**x)[i:i] // ERROR "slice: reuse 1st index" "slice: omit check for 1st index" "slice: result len == 0" 30 z = (**x)[1:i:i] // ERROR "slice: reuse 2nd index" "slice: omit check for 2nd index" "slice: result cap == result len" 31 z = (**x)[i:j:0] // ERROR "slice: omit check for 3rd index" 32 z = (**x)[i:0:j] // ERROR "slice: omit check for 2nd index" 33 z = (**x)[0:i:j] // ERROR "slice: omit check for 1st index" "slice: skip base adjustment for 1st index 0" 34 z = (**x)[0:] // ERROR "slice: omit slice operation" 35 z = (**x)[2:8] // ERROR "slice: omit check for 1st index" "slice: result len == 6" 36 z = (**x)[2:2] // ERROR "slice: omit check for 1st index" "slice: result len == 0" 37 z = (**x)[0:i] // ERROR "slice: omit check for 1st index" "slice: skip base adjustment for 1st index 0" 38 z = (**x)[2:i:8] // ERROR "slice: result cap == 6" 39 z = (**x)[i:2:i] // ERROR "slice: reuse 1st index" "slice: result cap == 0" "slice: skip base adjustment for cap == 0" 40 41 z = z[0:i] // ERROR "slice: omit check for 1st index" "slice: result cap not computed" "slice: skip base adjustment for 1st index 0" "slice: len-only update" 42 z = z[0:i : i+1] // ERROR "slice: omit check for 1st index" "slice: skip base adjustment for 1st index 0" "slice: len/cap-only update" 43 z = z[i : i+1] 44 45 println(z) 46 47 var zs string 48 zs = (**xs)[2:] // ERROR "slice: omit check for 2nd index" 49 zs = (**xs)[2:len(**xs)] // not yet: "slice: reuse len" "slice: omit check for 2nd index" 50 zs = (**xs)[i:i] // ERROR "slice: reuse 1st index" "slice: omit check for 1st index" "slice: result len == 0" "slice: skip base adjustment for string len == 0" 51 zs = (**xs)[0:] // ERROR "slice: omit slice operation" 52 zs = (**xs)[2:8] // ERROR "slice: omit check for 1st index" "slice: result len == 6" 53 zs = (**xs)[2:2] // ERROR "slice: omit check for 1st index" "slice: result len == 0" "slice: skip base adjustment for string len == 0" 54 zs = (**xs)[0:i] // ERROR "slice: omit check for 1st index" "slice: skip base adjustment for 1st index 0" 55 56 zs = zs[0:i] // ERROR "slice: omit check for 1st index" "slice: skip base adjustment for 1st index 0" "slice: len-only update" 57 zs = zs[i : i+1] 58 println(zs) 59 } 60