Debian Codegraph @

gopls/internal/analysis/writestring/testdata/src/a/a.go

raw symbols SCIP
1 package a
2
3 import (
4 "bufio"
5 "bytes"
6 "hash/maphash"
7 "os"
8 "strings"
9 )
10
11 func _(v string) {
12 var s strings.Builder
13 a := "a"
14 b := "b"
15 s.WriteString(a + b) // want "Inefficient string concatenation in call to WriteString"
16
17 var p *string
18 s.WriteString(a + ("b" + *p)) // want "Inefficient string concatenation in call to WriteString"
19
20 type S struct{ B bytes.Buffer }
21 var sb S
22 sb.B.WriteString(a + b) // want "Inefficient string concatenation in call to WriteString"
23
24 var bufw bufio.Writer
25 const k = "k"
26 bufw.WriteString(a + getString() + "b") // want "Inefficient string concatenation in call to WriteString"
27 bufw.WriteString(k + "b" + v + "c") // want "Inefficient string concatenation in call to WriteString"
28
29 var h maphash.Hash
30 h.WriteString(a + b) // want "Inefficient string concatenation in call to WriteString"
31
32 var buf bytes.Buffer
33 buf.WriteString(a) // nope - nothing to extract
34 buf.WriteString("a" + "b") // nope - no need to split because the concatenation is resolved without allocations
35 f(buf.WriteString(a + b)) // nope - inside function call
36 _, _ = buf.WriteString(a + b) // nope - inside assignment
37 getBuf().WriteString(a + b) // nope - function call in receiver
38 var bufs []bytes.Buffer
39 bufs[getIndex()].WriteString(a + b) // nope - index expression in receiver
40
41 var f *os.File
42 f.WriteString(a + b) // nope - os.File is not an allowed writer for this analyzer
43 }
44
45 func f(i int, err error) {}
46
47 func getIndex() int {
48 return 0
49 }
50
51 func getString() string {
52 return "string"
53 }
54
55 func getBuf() *bytes.Buffer { return &bytes.Buffer{} }