Skip to content

Commit

Permalink
checkers: fix caseOrder panic on undefined types (go-critic#808)
Browse files Browse the repository at this point in the history
  • Loading branch information
weeellz authored and quasilyte committed Feb 26, 2019
1 parent b937356 commit fc92fdb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions checkers/caseOrder_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func (c *caseOrderChecker) checkTypeSwitch(s *ast.TypeSwitchStmt) {
cc := cc.(*ast.CaseClause)
for _, x := range cc.List {
typ := c.ctx.TypesInfo.TypeOf(x)
if typ == nil {
c.warnTypeImpl(cc, x)
return
}
for _, iface := range ifaces {
if types.Implements(typ, iface.typ) {
c.warnTypeSwitch(cc, x, iface.node)
Expand All @@ -74,6 +78,10 @@ func (c *caseOrderChecker) warnTypeSwitch(cause, concrete, iface ast.Node) {
c.ctx.Warn(cause, "case %s must go before the %s case", concrete, iface)
}

func (c *caseOrderChecker) warnTypeImpl(cause, concrete ast.Node) {
c.ctx.Warn(cause, "type is not defined %s", concrete)
}

func (c *caseOrderChecker) checkSwitch(s *ast.SwitchStmt) {
// TODO(Quasilyte): can handle expression cases that overlap.
// Cases that have narrower value range should go before wider ones.
Expand Down
5 changes: 5 additions & 0 deletions checkers/testdata/caseOrder/positive_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ func typeSwitches(x interface{}) {
case *myReader:
default:
}

switch x.(type) {
/*! type is not defined myType */
case myType:
}
}
1 change: 1 addition & 0 deletions go-critic
Submodule go-critic added at b93735

0 comments on commit fc92fdb

Please sign in to comment.