|
| 1 | +//args: -Enilnil |
| 2 | +package testdata |
| 3 | + |
| 4 | +import ( |
| 5 | + "io" |
| 6 | + "unsafe" |
| 7 | +) |
| 8 | + |
| 9 | +type User struct{} |
| 10 | + |
| 11 | +func primitivePtr() (*int, error) { |
| 12 | + return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" |
| 13 | +} |
| 14 | + |
| 15 | +func structPtr() (*User, error) { |
| 16 | + return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" |
| 17 | +} |
| 18 | + |
| 19 | +func emptyStructPtr() (*struct{}, error) { |
| 20 | + return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" |
| 21 | +} |
| 22 | + |
| 23 | +func anonymousStructPtr() (*struct{ ID string }, error) { |
| 24 | + return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" |
| 25 | +} |
| 26 | + |
| 27 | +func chBi() (chan int, error) { |
| 28 | + return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" |
| 29 | +} |
| 30 | + |
| 31 | +func chIn() (chan<- int, error) { |
| 32 | + return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" |
| 33 | +} |
| 34 | + |
| 35 | +func chOut() (<-chan int, error) { |
| 36 | + return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" |
| 37 | +} |
| 38 | + |
| 39 | +func fun() (func(), error) { |
| 40 | + return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" |
| 41 | +} |
| 42 | + |
| 43 | +func funWithArgsAndResults() (func(a, b, c int) (int, int), error) { |
| 44 | + return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" |
| 45 | +} |
| 46 | + |
| 47 | +func iface() (interface{}, error) { |
| 48 | + return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" |
| 49 | +} |
| 50 | + |
| 51 | +func m1() (map[int]int, error) { |
| 52 | + return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" |
| 53 | +} |
| 54 | + |
| 55 | +func m2() (map[int]*User, error) { |
| 56 | + return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" |
| 57 | +} |
| 58 | + |
| 59 | +type Storage struct{} |
| 60 | + |
| 61 | +func (s *Storage) GetUser() (*User, error) { |
| 62 | + return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" |
| 63 | +} |
| 64 | + |
| 65 | +func ifReturn() (*User, error) { |
| 66 | + var s Storage |
| 67 | + if _, err := s.GetUser(); err != nil { |
| 68 | + return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" |
| 69 | + } |
| 70 | + return new(User), nil |
| 71 | +} |
| 72 | + |
| 73 | +func forReturn() (*User, error) { |
| 74 | + for { |
| 75 | + return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func multipleReturn() (*User, error) { |
| 80 | + var s Storage |
| 81 | + |
| 82 | + if _, err := s.GetUser(); err != nil { |
| 83 | + return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" |
| 84 | + } |
| 85 | + |
| 86 | + if _, err := s.GetUser(); err != nil { |
| 87 | + return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" |
| 88 | + } |
| 89 | + |
| 90 | + if _, err := s.GetUser(); err != nil { |
| 91 | + return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" |
| 92 | + } |
| 93 | + |
| 94 | + return new(User), nil |
| 95 | +} |
| 96 | + |
| 97 | +func nested() { |
| 98 | + _ = func() (*User, error) { |
| 99 | + return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" |
| 100 | + } |
| 101 | + |
| 102 | + _, _ = func() (*User, error) { |
| 103 | + return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" |
| 104 | + }() |
| 105 | +} |
| 106 | + |
| 107 | +func deeplyNested() { |
| 108 | + _ = func() { |
| 109 | + _ = func() int { |
| 110 | + _ = func() { |
| 111 | + _ = func() (*User, error) { |
| 112 | + _ = func() {} |
| 113 | + _ = func() int { return 0 } |
| 114 | + return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" |
| 115 | + } |
| 116 | + } |
| 117 | + return 0 |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +type ( |
| 123 | + StructPtrType *User |
| 124 | + PrimitivePtrType *int |
| 125 | + ChannelType chan int |
| 126 | + FuncType func(int) int |
| 127 | + Checker interface{ Check() } |
| 128 | +) |
| 129 | + |
| 130 | +func structPtrType() (StructPtrType, error) { |
| 131 | + return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" |
| 132 | +} |
| 133 | + |
| 134 | +func primitivePtrType() (PrimitivePtrType, error) { |
| 135 | + return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" |
| 136 | +} |
| 137 | + |
| 138 | +func channelType() (ChannelType, error) { |
| 139 | + return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" |
| 140 | +} |
| 141 | + |
| 142 | +func funcType() (FuncType, error) { |
| 143 | + return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" |
| 144 | +} |
| 145 | + |
| 146 | +func ifaceType() (Checker, error) { |
| 147 | + return nil, nil // ERROR "return both the `nil` error and invalid value: use a sentinel error instead" |
| 148 | +} |
| 149 | + |
| 150 | +func withoutArgs() {} |
| 151 | +func withoutError1() *User { return nil } |
| 152 | +func withoutError2() (*User, *User) { return nil, nil } |
| 153 | +func withoutError3() (*User, *User, *User) { return nil, nil, nil } |
| 154 | +func withoutError4() (*User, *User, *User, *User) { return nil, nil, nil, nil } |
| 155 | + |
| 156 | +// Unsupported. |
| 157 | + |
| 158 | +func invalidOrder() (error, *User) { return nil, nil } |
| 159 | +func withError3rd() (*User, bool, error) { return nil, false, nil } |
| 160 | +func withError4th() (*User, *User, *User, error) { return nil, nil, nil, nil } |
| 161 | +func unsafePtr() (unsafe.Pointer, error) { return nil, nil } |
| 162 | +func uintPtr() (uintptr, error) { return 0, nil } |
| 163 | +func slice() ([]int, error) { return nil, nil } |
| 164 | +func ifaceExtPkg() (io.Closer, error) { return nil, nil } |
| 165 | + |
| 166 | +func implicitNil1() (*User, error) { |
| 167 | + err := (error)(nil) |
| 168 | + return nil, err |
| 169 | +} |
| 170 | + |
| 171 | +func implicitNil2() (*User, error) { |
| 172 | + err := io.EOF |
| 173 | + err = nil |
| 174 | + return nil, err |
| 175 | +} |
| 176 | + |
| 177 | +func implicitNil3() (*User, error) { |
| 178 | + return nil, wrap(nil) |
| 179 | +} |
| 180 | +func wrap(err error) error { return err } |
0 commit comments