Skip to content

Commit 01f11fb

Browse files
committed
Handle untyped nil
1 parent 92ba505 commit 01f11fb

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

opaque/opaque.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,15 @@ func (r *runner) run(pass *analysis.Pass) (interface{}, error) {
157157
}
158158

159159
typ := pass.TypesInfo.TypeOf(res)
160+
isNilStmt := isUntypedNil(typ)
160161

161162
if r.debug {
162-
fmt.Printf(" Ident type: %v %v interface=%t\n", typ, reflect.TypeOf(typ), types.IsInterface(typ))
163+
fmt.Printf(" Ident type: %v %v interface=%t, untypedNil=%t\n", typ, reflect.TypeOf(typ), types.IsInterface(typ), isNilStmt)
163164
}
164165

165-
retStmtTypes[i][typ] = struct{}{}
166+
if !isNilStmt {
167+
retStmtTypes[i][typ] = struct{}{}
168+
}
166169
case *ast.UnaryExpr:
167170
if r.debug {
168171
fmt.Printf(" UnaryExpr X: %v \n", res.X)
@@ -302,6 +305,14 @@ func (r *runner) run(pass *analysis.Pass) (interface{}, error) {
302305
return nil, nil
303306
}
304307

308+
func isUntypedNil(typ types.Type) bool {
309+
if b, ok := typ.(*types.Basic); ok {
310+
return b.Kind() == types.UntypedNil
311+
}
312+
313+
return false
314+
}
315+
305316
func positionStr(idx int) string {
306317
switch idx {
307318
case 0:

opaque/testdata/src/a/a.go

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package app
22

3+
import "errors"
4+
35
type Server interface {
46
Serve() error
57
}
@@ -24,6 +26,13 @@ func NewServer3(addr string) Server { // want "NewServer3 function return Server
2426
return &server{addr: addr}
2527
}
2628

29+
func NewServer4(addr string) (Server, error) { // want "NewServer4 function return Server interface at the 1st result, abstract a single concrete implementation of \\*server"
30+
if addr == "" {
31+
return nil, errors.New("addr cannot be nil")
32+
}
33+
return &server{addr: addr}, nil
34+
}
35+
2736
func newServer(addr string) *server {
2837
return &server{addr: addr}
2938
}

opaque/testdata/src/a/a.go.golden

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package app
22

3+
import "errors"
4+
35
type Server interface {
46
Serve() error
57
}
@@ -24,6 +26,13 @@ func NewServer3(addr string) *server { // want "NewServer3 function return Serve
2426
return &server{addr: addr}
2527
}
2628

29+
func NewServer4(addr string) (*server, error) { // want "NewServer4 function return Server interface at the 1st result, abstract a single concrete implementation of \\*server"
30+
if addr == "" {
31+
return nil, errors.New("addr cannot be nil")
32+
}
33+
return &server{addr: addr}, nil
34+
}
35+
2736
func newServer(addr string) *server {
2837
return &server{addr: addr}
2938
}

0 commit comments

Comments
 (0)