Skip to content

Commit

Permalink
Refactor check to switchComparesNonNil()
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelUrman authored and polyfloyd committed Sep 16, 2022
1 parent 8587396 commit 2a7b31d
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions errorlint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,31 +189,13 @@ func LintErrorComparisons(fset *token.FileSet, info *TypesInfoExt) []Lint {
continue
}

hasErrorCase := false
for _, caseBlock := range switchStmt.Body.List {
caseClause, ok := caseBlock.(*ast.CaseClause)
if !ok {
continue
}
for _, clause := range caseClause.List {
switch clause := clause.(type) {
case nil:
continue // default label is safe
case *ast.Ident:
if clause.Name != "nil" {
hasErrorCase = true
}
}
}
}
if !hasErrorCase {
continue
if switchComparesNonNil(switchStmt) {
lints = append(lints, Lint{
Message: "switch on an error will fail on wrapped errors. Use errors.Is to check for specific errors",
Pos: switchStmt.Pos(),
})
}

lints = append(lints, Lint{
Message: "switch on an error will fail on wrapped errors. Use errors.Is to check for specific errors",
Pos: switchStmt.Pos(),
})
}

return lints
Expand Down Expand Up @@ -259,6 +241,31 @@ func isNodeInErrorIsFunc(info *TypesInfoExt, node ast.Node) bool {
return true
}

// switchComparesNonNil returns true if one of its clauses compares by value.
func switchComparesNonNil(switchStmt *ast.SwitchStmt) bool {
for _, caseBlock := range switchStmt.Body.List {
caseClause, ok := caseBlock.(*ast.CaseClause)
if !ok {
continue
}
for _, clause := range caseClause.List {
switch clause := clause.(type) {
case nil:
// default label is safe
continue
case *ast.Ident:
// `case nil` is safe
if clause.Name == "nil" {
continue
}
}
// anything else (including an Ident other than nil) isn't safe
return true
}
}
return false
}

func LintErrorTypeAssertions(fset *token.FileSet, info types.Info) []Lint {
lints := []Lint{}

Expand Down

0 comments on commit 2a7b31d

Please sign in to comment.