Skip to content
This repository was archived by the owner on Nov 10, 2025. It is now read-only.

Commit aede097

Browse files
committed
refactor: Invert switchComparesNonNil branch to decrease indentation
1 parent 5095374 commit aede097

File tree

1 file changed

+69
-67
lines changed

1 file changed

+69
-67
lines changed

errorlint/lint.go

Lines changed: 69 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -269,86 +269,88 @@ func LintErrorComparisons(info *TypesInfoExt) []analysis.Diagnostic {
269269
continue
270270
}
271271

272-
if switchComparesNonNil(switchStmt) {
273-
diagnostic := analysis.Diagnostic{
274-
Message: "switch on an error will fail on wrapped errors. Use errors.Is to check for specific errors",
275-
Pos: problematicCaseClause.Pos(),
276-
}
272+
if !switchComparesNonNil(switchStmt) {
273+
continue
274+
}
277275

278-
// Create a simpler version of the fix for switch statements
279-
// We'll transform: switch err { case ErrX: ... }
280-
// To: switch { case errors.Is(err, ErrX): ... }
276+
diagnostic := analysis.Diagnostic{
277+
Message: "switch on an error will fail on wrapped errors. Use errors.Is to check for specific errors",
278+
Pos: problematicCaseClause.Pos(),
279+
}
281280

282-
// Create a new switch statement with an empty tag
283-
newSwitchStmt := &ast.SwitchStmt{
284-
Init: switchStmt.Init,
285-
Tag: nil, // Empty tag for the switch.
286-
Body: &ast.BlockStmt{
287-
List: make([]ast.Stmt, len(switchStmt.Body.List)),
288-
},
289-
}
281+
// Create a simpler version of the fix for switch statements
282+
// We'll transform: switch err { case ErrX: ... }
283+
// To: switch { case errors.Is(err, ErrX): ... }
290284

291-
// Convert each case to use errors.Is.
292-
switchTagExpr := switchStmt.Tag // The error variable being checked.
293-
for i, stmt := range switchStmt.Body.List {
294-
origCaseClause := stmt.(*ast.CaseClause)
285+
// Create a new switch statement with an empty tag
286+
newSwitchStmt := &ast.SwitchStmt{
287+
Init: switchStmt.Init,
288+
Tag: nil, // Empty tag for the switch.
289+
Body: &ast.BlockStmt{
290+
List: make([]ast.Stmt, len(switchStmt.Body.List)),
291+
},
292+
}
295293

296-
// Create a new case clause.
297-
newCaseClause := &ast.CaseClause{
298-
Body: origCaseClause.Body,
299-
}
294+
// Convert each case to use errors.Is.
295+
switchTagExpr := switchStmt.Tag // The error variable being checked.
296+
for i, stmt := range switchStmt.Body.List {
297+
origCaseClause := stmt.(*ast.CaseClause)
300298

301-
// If this is a default case (no expressions), keep it as-is.
302-
if len(origCaseClause.List) == 0 {
303-
newCaseClause.List = nil // Default case.
304-
newSwitchStmt.Body.List[i] = newCaseClause
305-
continue
306-
}
299+
// Create a new case clause.
300+
newCaseClause := &ast.CaseClause{
301+
Body: origCaseClause.Body,
302+
}
307303

308-
newCaseClause.List = make([]ast.Expr, 0, len(origCaseClause.List))
309-
310-
// Convert each case expression.
311-
for _, caseExpr := range origCaseClause.List {
312-
if isNil(caseExpr) {
313-
// Keep nil checks as is: case err == nil:
314-
newCaseClause.List = append(newCaseClause.List,
315-
&ast.BinaryExpr{
316-
X: switchTagExpr,
317-
Op: token.EQL,
318-
Y: caseExpr,
319-
})
320-
continue
321-
}
322-
// Replace err == ErrX with errors.Is(err, ErrX).
304+
// If this is a default case (no expressions), keep it as-is.
305+
if len(origCaseClause.List) == 0 {
306+
newCaseClause.List = nil // Default case.
307+
newSwitchStmt.Body.List[i] = newCaseClause
308+
continue
309+
}
310+
311+
newCaseClause.List = make([]ast.Expr, 0, len(origCaseClause.List))
312+
313+
// Convert each case expression.
314+
for _, caseExpr := range origCaseClause.List {
315+
if isNil(caseExpr) {
316+
// Keep nil checks as is: case err == nil:
323317
newCaseClause.List = append(newCaseClause.List,
324-
&ast.CallExpr{
325-
Fun: &ast.SelectorExpr{
326-
X: ast.NewIdent("errors"),
327-
Sel: ast.NewIdent("Is"),
328-
},
329-
Args: []ast.Expr{switchTagExpr, caseExpr},
318+
&ast.BinaryExpr{
319+
X: switchTagExpr,
320+
Op: token.EQL,
321+
Y: caseExpr,
330322
})
323+
continue
331324
}
332-
333-
newSwitchStmt.Body.List[i] = newCaseClause
325+
// Replace err == ErrX with errors.Is(err, ErrX).
326+
newCaseClause.List = append(newCaseClause.List,
327+
&ast.CallExpr{
328+
Fun: &ast.SelectorExpr{
329+
X: ast.NewIdent("errors"),
330+
Sel: ast.NewIdent("Is"),
331+
},
332+
Args: []ast.Expr{switchTagExpr, caseExpr},
333+
})
334334
}
335335

336-
// Print the modified AST to get the fix text.
337-
var buf bytes.Buffer
338-
printer.Fprint(&buf, token.NewFileSet(), newSwitchStmt)
339-
fixText := buf.String()
336+
newSwitchStmt.Body.List[i] = newCaseClause
337+
}
340338

341-
diagnostic.SuggestedFixes = []analysis.SuggestedFix{{
342-
Message: "Convert to errors.Is() for error comparisons",
343-
TextEdits: []analysis.TextEdit{{
344-
Pos: switchStmt.Pos(),
345-
End: switchStmt.End(),
346-
NewText: []byte(fixText),
347-
}},
348-
}}
339+
// Print the modified AST to get the fix text.
340+
var buf bytes.Buffer
341+
printer.Fprint(&buf, token.NewFileSet(), newSwitchStmt)
342+
fixText := buf.String()
349343

350-
lints = append(lints, diagnostic)
351-
}
344+
diagnostic.SuggestedFixes = []analysis.SuggestedFix{{
345+
Message: "Convert to errors.Is() for error comparisons",
346+
TextEdits: []analysis.TextEdit{{
347+
Pos: switchStmt.Pos(),
348+
End: switchStmt.End(),
349+
NewText: []byte(fixText),
350+
}},
351+
}}
352+
353+
lints = append(lints, diagnostic)
352354
}
353355

354356
return lints

0 commit comments

Comments
 (0)