Debian Codegraph @

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

raw
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)
16 s.WriteString(b) // want "Inefficient string concatenation in call to WriteString"
17
18 var p *string
19 s.WriteString(a)
20 s.WriteString("b")
21 s.WriteString(*p) // want "Inefficient string concatenation in call to WriteString"
22
23 type S struct{ B bytes.Buffer }
24 var sb S
25 sb.B.WriteString(a)
26 sb.B.WriteString(b) // want "Inefficient string concatenation in call to WriteString"
27
28 var bufw bufio.Writer
29 const k = "k"
30 bufw.WriteString(a)
31 bufw.WriteString(getString())
32 bufw.WriteString("b") // want "Inefficient string concatenation in call to WriteString"
33 bufw.WriteString(k + "b")
34 bufw.WriteString(v)
35 bufw.WriteString("c") // want "Inefficient string concatenation in call to WriteString"
36
37 var h maphash.Hash
38 h.WriteString(a)
39 h.WriteString(b) // want "Inefficient string concatenation in call to WriteString"
40
41 var buf bytes.Buffer
42 buf.WriteString(a) // nope - nothing to extract
43 buf.WriteString("a" + "b") // nope - no need to split because the concatenation is resolved without allocations
44 f(buf.WriteString(a + b)) // nope - inside function call
45 _, _ = buf.WriteString(a + b) // nope - inside assignment
46 getBuf().WriteString(a + b) // nope - function call in receiver
47 var bufs []bytes.Buffer
48 bufs[getIndex()].WriteString(a + b) // nope - index expression in receiver
49
50 var f *os.File
51 f.WriteString(a + b) // nope - os.File is not an allowed writer for this analyzer
52 }
53
54 func f(i int, err error) {}
55
56 func getIndex() int {
57 return 0
58 }
59
60 func getString() string {
61 return "string"
62 }
63
64 func getBuf() *bytes.Buffer { return &bytes.Buffer{} }
65