honnef.co/go/tools@v0.5.0-0.dev.0.20240520180541-dcae280a5e87/simple/s1030/testdata/src/example.com/CheckBytesBufferConversions/LintBytesBufferConversions.go (about) 1 package pkg 2 3 import ( 4 "bytes" 5 ) 6 7 type AliasByte = byte 8 type AliasSlice = []byte 9 type AliasSlice2 = []AliasByte 10 11 func fn() { 12 buf := bytes.NewBufferString("str") 13 _ = string(buf.Bytes()) //@ diag(`should use buf.String() instead of string(buf.Bytes())`) 14 _ = []byte(buf.String()) //@ diag(`should use buf.Bytes() instead of []byte(buf.String())`) 15 16 m := map[string]*bytes.Buffer{"key": buf} 17 _ = string(m["key"].Bytes()) //@ diag(`should use m["key"].String() instead of string(m["key"].Bytes())`) 18 _ = []byte(m["key"].String()) //@ diag(`should use m["key"].Bytes() instead of []byte(m["key"].String())`) 19 20 _ = []AliasByte(m["key"].String()) //@ diag(`should use m["key"].Bytes() instead of []AliasByte(m["key"].String())`) 21 _ = AliasSlice(m["key"].String()) //@ diag(`should use m["key"].Bytes() instead of AliasSlice(m["key"].String())`) 22 _ = AliasSlice2(m["key"].String()) //@ diag(`should use m["key"].Bytes() instead of AliasSlice2(m["key"].String())`) 23 24 var m2 map[string]int 25 _ = m2[string(buf.Bytes())] // no warning, this is more efficient than buf.String() 26 27 string := func(_ interface{}) interface{} { 28 return nil 29 } 30 _ = string(m["key"].Bytes()) 31 }