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