Skip to content

Commit

Permalink
ensure selectorExpr.X is package name
Browse files Browse the repository at this point in the history
see test case
  • Loading branch information
nishanths committed Aug 11, 2020
1 parent 8866003 commit 6cf413a
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@

# binary
cmd/exhaustive/exhaustive
exhaustive
10 changes: 10 additions & 0 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ func checkMapLiterals(pass *analysis.Pass, inspect *inspector.Inspector, comment
if !ok {
continue
}

// ensure X is package identifier
ident, ok := selExpr.X.(*ast.Ident)
if !ok {
continue
}
if !isPackageNameIdentifier(pass, ident) {
continue
}

delete(hitlist, selExpr.Sel.Name)
}
}
Expand Down
19 changes: 19 additions & 0 deletions switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ func checkSwitchStatements(pass *analysis.Pass, inspect *inspector.Inspector, co
if !ok {
continue
}

// ensure X is package identifier
ident, ok := selExpr.X.(*ast.Ident)
if !ok {
continue
}
if !isPackageNameIdentifier(pass, ident) {
continue
}

delete(hitlist, selExpr.Sel.Name)
}
}
Expand All @@ -120,6 +130,15 @@ func checkSwitchStatements(pass *analysis.Pass, inspect *inspector.Inspector, co
})
}

func isPackageNameIdentifier(pass *analysis.Pass, ident *ast.Ident) bool {
obj := pass.TypesInfo.ObjectOf(ident)
if obj == nil {
return false
}
_, ok := obj.(*types.PkgName)
return ok
}

func hitlistFromEnumMembers(enumMembers []string, checkUnexported bool) map[string]struct{} {
hitlist := make(map[string]struct{})
for _, m := range enumMembers {
Expand Down
24 changes: 24 additions & 0 deletions testdata/playground/exp/exp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package exp

// NOTE: Feel free to delete code below. This is a temporary scratchpad for use
// when working on bugfixes or new code for exhaustive.

import "github.com/nishanths/exhaustive/testdata/playground/x"

func _o() {
// Selector isn't of the form "enumPkg.enumMember"

type holdsW struct {
W x.Direction
}

var d x.Direction
var h holdsW

switch d {
case x.N:
case x.E:
case x.S:
case h.W:
}
}
17 changes: 17 additions & 0 deletions testdata/src/switch/x/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,20 @@ func _n() {
case Desc:
}
}

func _o() {
// Selector isn't of the form "enumPkg.enumMember"

type holdsMollusca struct {
Mollusca bar.Phylum // can hold any Phylum value
}

var p bar.Phylum
var h holdsMollusca

switch p { // want "missing cases in switch of type bar.Phylum: Mollusca"
case bar.Chordata:
case bar.Echinodermata:
case h.Mollusca:
}
}

0 comments on commit 6cf413a

Please sign in to comment.