Skip to content

Commit fc92fdb

Browse files
weeellzquasilyte
authored andcommitted
checkers: fix caseOrder panic on undefined types (go-critic#808)
Fixes go-critic#781
1 parent b937356 commit fc92fdb

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

checkers/caseOrder_checker.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ func (c *caseOrderChecker) checkTypeSwitch(s *ast.TypeSwitchStmt) {
5757
cc := cc.(*ast.CaseClause)
5858
for _, x := range cc.List {
5959
typ := c.ctx.TypesInfo.TypeOf(x)
60+
if typ == nil {
61+
c.warnTypeImpl(cc, x)
62+
return
63+
}
6064
for _, iface := range ifaces {
6165
if types.Implements(typ, iface.typ) {
6266
c.warnTypeSwitch(cc, x, iface.node)
@@ -74,6 +78,10 @@ func (c *caseOrderChecker) warnTypeSwitch(cause, concrete, iface ast.Node) {
7478
c.ctx.Warn(cause, "case %s must go before the %s case", concrete, iface)
7579
}
7680

81+
func (c *caseOrderChecker) warnTypeImpl(cause, concrete ast.Node) {
82+
c.ctx.Warn(cause, "type is not defined %s", concrete)
83+
}
84+
7785
func (c *caseOrderChecker) checkSwitch(s *ast.SwitchStmt) {
7886
// TODO(Quasilyte): can handle expression cases that overlap.
7987
// Cases that have narrower value range should go before wider ones.

checkers/testdata/caseOrder/positive_tests.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,9 @@ func typeSwitches(x interface{}) {
4646
case *myReader:
4747
default:
4848
}
49+
50+
switch x.(type) {
51+
/*! type is not defined myType */
52+
case myType:
53+
}
4954
}

go-critic

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit b9373569160643640c0cfa345c5b6c8d549ad36d

0 commit comments

Comments
 (0)