Source file test/escape_mutations.go
1 // errorcheck -0 -m -d=escapemutationscalls,zerocopy -l 2 3 // Copyright 2023 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 import "fmt" 10 11 type B struct { 12 x int 13 px *int 14 pb *B 15 } 16 17 func F1(b *B) { // ERROR "mutates param: b derefs=0" 18 b.x = 1 19 } 20 21 func F2(b *B) { // ERROR "mutates param: b derefs=1" 22 *b.px = 1 23 } 24 25 func F2a(b *B) { // ERROR "mutates param: b derefs=0" 26 b.px = nil 27 } 28 29 func F3(b *B) { // ERROR "leaking param: b" 30 fmt.Println(b) // ERROR "\.\.\. argument does not escape" 31 } 32 33 func F4(b *B) { // ERROR "leaking param content: b" 34 fmt.Println(*b) // ERROR "\.\.\. argument does not escape" "\*b escapes to heap" 35 } 36 37 func F4a(b *B) { // ERROR "leaking param content: b" "mutates param: b derefs=0" 38 b.x = 2 39 fmt.Println(*b) // ERROR "\.\.\. argument does not escape" "\*b escapes to heap" 40 } 41 42 func F5(b *B) { // ERROR "leaking param: b" 43 sink = b 44 } 45 46 func F6(b *B) int { // ERROR "b does not escape, mutate, or call" 47 return b.x 48 } 49 50 var sink any 51 52 func M() { 53 var b B // ERROR "moved to heap: b" 54 F1(&b) 55 F2(&b) 56 F2a(&b) 57 F3(&b) 58 F4(&b) 59 } 60 61 func g(s string) { // ERROR "s does not escape, mutate, or call" 62 sink = &([]byte(s))[10] // ERROR "\(\[\]byte\)\(s\) escapes to heap" 63 } 64 65 func h(out []byte, s string) { // ERROR "mutates param: out derefs=0" "s does not escape, mutate, or call" 66 copy(out, []byte(s)) // ERROR "zero-copy string->\[\]byte conversion" "\(\[\]byte\)\(s\) does not escape" 67 } 68 69 func i(s string) byte { // ERROR "s does not escape, mutate, or call" 70 p := []byte(s) // ERROR "zero-copy string->\[\]byte conversion" "\(\[\]byte\)\(s\) does not escape" 71 return p[20] 72 } 73 74 func j(s string, x byte) { // ERROR "s does not escape, mutate, or call" 75 p := []byte(s) // ERROR "\(\[\]byte\)\(s\) does not escape" 76 p[20] = x 77 } 78