Skip to content

Commit

Permalink
Handle untyped nil
Browse files Browse the repository at this point in the history
  • Loading branch information
uudashr committed Jan 8, 2025
1 parent 92ba505 commit 01f11fb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
15 changes: 13 additions & 2 deletions opaque/opaque.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,15 @@ func (r *runner) run(pass *analysis.Pass) (interface{}, error) {
}

typ := pass.TypesInfo.TypeOf(res)
isNilStmt := isUntypedNil(typ)

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

retStmtTypes[i][typ] = struct{}{}
if !isNilStmt {
retStmtTypes[i][typ] = struct{}{}
}
case *ast.UnaryExpr:
if r.debug {
fmt.Printf(" UnaryExpr X: %v \n", res.X)
Expand Down Expand Up @@ -302,6 +305,14 @@ func (r *runner) run(pass *analysis.Pass) (interface{}, error) {
return nil, nil
}

func isUntypedNil(typ types.Type) bool {
if b, ok := typ.(*types.Basic); ok {
return b.Kind() == types.UntypedNil
}

return false
}

func positionStr(idx int) string {
switch idx {
case 0:
Expand Down
9 changes: 9 additions & 0 deletions opaque/testdata/src/a/a.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package app

import "errors"

type Server interface {
Serve() error
}
Expand All @@ -24,6 +26,13 @@ func NewServer3(addr string) Server { // want "NewServer3 function return Server
return &server{addr: addr}
}

func NewServer4(addr string) (Server, error) { // want "NewServer4 function return Server interface at the 1st result, abstract a single concrete implementation of \\*server"
if addr == "" {
return nil, errors.New("addr cannot be nil")
}
return &server{addr: addr}, nil
}

func newServer(addr string) *server {
return &server{addr: addr}
}
Expand Down
9 changes: 9 additions & 0 deletions opaque/testdata/src/a/a.go.golden
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package app

import "errors"

type Server interface {
Serve() error
}
Expand All @@ -24,6 +26,13 @@ func NewServer3(addr string) *server { // want "NewServer3 function return Serve
return &server{addr: addr}
}

func NewServer4(addr string) (*server, error) { // want "NewServer4 function return Server interface at the 1st result, abstract a single concrete implementation of \\*server"
if addr == "" {
return nil, errors.New("addr cannot be nil")
}
return &server{addr: addr}, nil
}

func newServer(addr string) *server {
return &server{addr: addr}
}
Expand Down

0 comments on commit 01f11fb

Please sign in to comment.