Skip to content

Commit

Permalink
Support aliased types (#9)
Browse files Browse the repository at this point in the history
* Support aliased types

* Introduce  interface
  • Loading branch information
Antonboom authored Mar 29, 2023
1 parent 803825e commit c327615
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
18 changes: 14 additions & 4 deletions pkg/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,17 @@ var (
}
)

type typeSpecByName map[string]*ast.TypeSpec
type typeSpecByName map[string]typer

func (n *nilNil) run(pass *analysis.Pass) (interface{}, error) {
insp := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)

typeSpecs := typeSpecByName{}
typeSpecs := typeSpecByName{
"any": newTyper(new(ast.InterfaceType)),
}
insp.Preorder(types, func(node ast.Node) {
t := node.(*ast.TypeSpec)
typeSpecs[t.Name.Name] = t
typeSpecs[t.Name.Name] = newTyper(t.Type)
})

var fs funcTypeStack
Expand Down Expand Up @@ -125,7 +127,7 @@ func (n *nilNil) isDangerNilType(t ast.Expr, typeSpecs typeSpecByName) bool {

case *ast.Ident:
if t, ok := typeSpecs[v.Name]; ok {
return n.isDangerNilType(t.Type, nil)
return n.isDangerNilType(t.Type(), typeSpecs)
}
}
return false
Expand All @@ -146,3 +148,11 @@ func isIdent(n ast.Node, name string) bool {
}
return i.Name == name
}

type typer interface {
Type() ast.Expr
}

func newTyper(t ast.Expr) typer { return typerImpl{t: t} } //
type typerImpl struct{ t ast.Expr } //
func (ti typerImpl) Type() ast.Expr { return ti.t }
10 changes: 10 additions & 0 deletions pkg/analyzer/testdata/src/examples/positive.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func iface() (interface{}, error) {
return nil, nil // want "return both the `nil` error and invalid value: use a sentinel error instead"
}

func anyType() (any, error) {
return nil, nil // want "return both the `nil` error and invalid value: use a sentinel error instead"
}

func m1() (map[int]int, error) {
return nil, nil // want "return both the `nil` error and invalid value: use a sentinel error instead"
}
Expand All @@ -50,6 +54,12 @@ func m2() (map[int]*User, error) {
return nil, nil // want "return both the `nil` error and invalid value: use a sentinel error instead"
}

type mapAlias = map[int]*User

func m3() (mapAlias, error) {
return nil, nil // want "return both the `nil` error and invalid value: use a sentinel error instead"
}

type Storage struct{}

func (s *Storage) GetUser() (*User, error) {
Expand Down
6 changes: 6 additions & 0 deletions pkg/analyzer/testdata/src/examples/positive_with_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ func funcType() (FuncType, error) {
func ifaceType() (Checker, error) {
return nil, nil // want "return both the `nil` error and invalid value: use a sentinel error instead"
}

type checkerAlias = Checker

func ifaceTypeAliased() (checkerAlias, error) {
return nil, nil // want "return both the `nil` error and invalid value: use a sentinel error instead"
}

0 comments on commit c327615

Please sign in to comment.