/
test.go
142 lines (113 loc) · 2.75 KB
/
test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"bytes"
"fmt"
"io/ioutil"
"math/rand"
mrand "math/rand"
)
func a() error {
fmt.Println("this function returns an error") // UNCHECKED
return nil
}
func b() (int, error) {
fmt.Println("this function returns an int and an error") // UNCHECKED
return 0, nil
}
func c() int {
fmt.Println("this function returns an int") // UNCHECKED
return 7
}
func rec() {
defer func() {
recover() // UNCHECKED
_ = recover() // BLANK
}()
defer recover() // UNCHECKED
}
type MyError string
func (e MyError) Error() string {
return string(e)
}
func customError() error {
return MyError("an error occurred")
}
func customConcreteError() MyError {
return MyError("an error occurred")
}
func customConcreteErrorTuple() (int, MyError) {
return 0, MyError("an error occurred")
}
type MyPointerError string
func (e *MyPointerError) Error() string {
return string(*e)
}
func customPointerError() *MyPointerError {
e := MyPointerError("an error occurred")
return &e
}
func customPointerErrorTuple() (int, *MyPointerError) {
e := MyPointerError("an error occurred")
return 0, &e
}
func main() {
// Single error return
_ = a() // BLANK
a() // UNCHECKED
// Return another value and an error
_, _ = b() // BLANK
b() // UNCHECKED
// Return a custom error type
_ = customError() // BLANK
customError() // UNCHECKED
// Return a custom concrete error type
_ = customConcreteError() // BLANK
customConcreteError() // UNCHECKED
_, _ = customConcreteErrorTuple() // BLANK
customConcreteErrorTuple() // UNCHECKED
// Return a custom pointer error type
_ = customPointerError() // BLANK
customPointerError() // UNCHECKED
_, _ = customPointerErrorTuple() // BLANK
customPointerErrorTuple() // UNCHECKED
// Method with a single error return
x := t{}
_ = x.a() // BLANK
x.a() // UNCHECKED
// Method call on a struct member
y := u{x}
_ = y.t.a() // BLANK
y.t.a() // UNCHECKED
m1 := map[string]func() error{"a": a}
_ = m1["a"]() // BLANK
m1["a"]() // UNCHECKED
// Additional cases for assigning errors to blank identifier
z, _ := b() // BLANK
_, w := a(), 5 // BLANK
// Assign non error to blank identifier
_ = c()
_ = z + w // Avoid complaints about unused variables
// Type assertions
var i interface{}
s1 := i.(string) // ASSERT
s1 = i.(string) // ASSERT
s2, _ := i.(string) // ASSERT
s2, _ = i.(string) // ASSERT
s3, ok := i.(string)
s3, ok = i.(string)
switch s4 := i.(type) {
case string:
_ = s4
}
_, _, _, _ = s1, s2, s3, ok
// Goroutine
go a() // UNCHECKED
defer a() // UNCHECKED
b1 := bytes.Buffer{}
b2 := &bytes.Buffer{}
b1.Write(nil)
b2.Write(nil)
rand.Read(nil)
mrand.Read(nil)
ioutil.ReadFile("main.go") // UNCHECKED
}