@@ -156,10 +156,14 @@ func LintErrorComparisons(fset *token.FileSet, info *TypesInfoExt) []Lint {
156156 if ! isErrorComparison (info .Info , binExpr ) {
157157 continue
158158 }
159-
159+ // Some errors that are returned from some functions are exempt.
160160 if isAllowedErrorComparison (info , binExpr ) {
161161 continue
162162 }
163+ // Comparisons that happen in `func (type) Is(error) bool` are okay.
164+ if isNodeInErrorIsFunc (info , binExpr ) {
165+ continue
166+ }
163167
164168 lints = append (lints , Lint {
165169 Message : fmt .Sprintf ("comparing with %s will fail on wrapped errors. Use errors.Is to check for a specific error" , binExpr .Op ),
@@ -181,6 +185,9 @@ func LintErrorComparisons(fset *token.FileSet, info *TypesInfoExt) []Lint {
181185 if tagType .Type .String () != "error" {
182186 continue
183187 }
188+ if isNodeInErrorIsFunc (info , switchStmt ) {
189+ continue
190+ }
184191
185192 lints = append (lints , Lint {
186193 Message : "switch on an error will fail on wrapped errors. Use errors.Is to check for specific errors" ,
@@ -207,6 +214,30 @@ func isErrorComparison(info types.Info, binExpr *ast.BinaryExpr) bool {
207214 return tx .Type .String () == "error" || ty .Type .String () == "error"
208215}
209216
217+ func isNodeInErrorIsFunc (info * TypesInfoExt , node ast.Node ) bool {
218+ funcDecl := info .ContainingFuncDecl (node )
219+ if funcDecl == nil {
220+ return false
221+ }
222+
223+ if funcDecl .Name .Name != "Is" {
224+ return false
225+ }
226+ if funcDecl .Recv == nil {
227+ return false
228+ }
229+ // There should be 1 argument of type error.
230+ if ii := funcDecl .Type .Params .List ; len (ii ) != 1 || info .Types [ii [0 ].Type ].Type .String () != "error" {
231+ return false
232+ }
233+ // The return type should be bool.
234+ if ii := funcDecl .Type .Results .List ; len (ii ) != 1 || info .Types [ii [0 ].Type ].Type .String () != "bool" {
235+ return false
236+ }
237+
238+ return true
239+ }
240+
210241func LintErrorTypeAssertions (fset * token.FileSet , info types.Info ) []Lint {
211242 lints := []Lint {}
212243
0 commit comments