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

Commit 7eebac1

Browse files
committed
feat: Look for multiple %w verbs as these are valid starting with Go 1.20
1 parent 0fcf595 commit 7eebac1

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

errorlint/lint.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ func LintFmtErrorfCalls(fset *token.FileSet, info types.Info) []Lint {
4141
continue
4242
}
4343

44-
// For any arguments that are errors, check whether the wrapping verb
45-
// is used. Only one %w verb may be used in a single format string at a
46-
// time, so we stop after finding a correct %w.
44+
// For any arguments that are errors, check whether the wrapping verb is used. %w may occur
45+
// for multiple errors in one Errorf invocation. We raise an issue if at least one error
46+
// does not have a corresponding wrapping verb.
4747
var lintArg ast.Expr
4848
args := call.Args[1:]
4949
for i := 0; i < len(args) && i < len(formatVerbs); i++ {
@@ -52,12 +52,12 @@ func LintFmtErrorfCalls(fset *token.FileSet, info types.Info) []Lint {
5252
}
5353

5454
if formatVerbs[i] == "w" {
55-
lintArg = nil
56-
break
55+
continue
5756
}
5857

5958
if lintArg == nil {
6059
lintArg = args[i]
60+
break
6161
}
6262
}
6363
if lintArg != nil {
@@ -85,8 +85,8 @@ func isErrorStringCall(info types.Info, expr ast.Expr) bool {
8585
}
8686

8787
// printfFormatStringVerbs returns a normalized list of all the verbs that are used per argument to
88-
// the printf function. The index of each returned element corresponds to index of the respective
89-
// argument.
88+
// the printf function. The index of each returned element corresponds to the index of the
89+
// respective argument.
9090
func printfFormatStringVerbs(info types.Info, call *ast.CallExpr) ([]string, bool) {
9191
if len(call.Args) <= 1 {
9292
return nil, false

errorlint/testdata/src/fmterrorf/fmterrorf.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,25 @@ func DoubleNonWrappingVerb() error {
2020
return fmt.Errorf("%v %v", err, err) // want "non-wrapping format verb for fmt.Errorf. Use `%w` to format errors"
2121
}
2222

23-
func ErrorAtLeastOneWrap() error {
23+
func ErrorOneWrap() error {
2424
err1 := errors.New("oops1")
2525
err2 := errors.New("oops2")
2626
err3 := errors.New("oops3")
27-
return fmt.Errorf("%v, %w, %v", err1, err2, err3)
27+
return fmt.Errorf("%v, %w, %v", err1, err2, err3) // want "non-wrapping format verb for fmt.Errorf. Use `%w` to format errors"
2828
}
2929

30-
func ErrorAtLeastOneWrapWithCustomError() error {
30+
func ErrorMultipleWraps() error {
31+
err1 := errors.New("oops1")
32+
err2 := errors.New("oops2")
33+
err3 := errors.New("oops3")
34+
return fmt.Errorf("%w, %w, %w", err1, err2, err3)
35+
}
36+
37+
func ErrorMultipleWrapsWithCustomError() error {
3138
err1 := errors.New("oops1")
3239
err2 := MyError{}
3340
err3 := errors.New("oops3")
34-
return fmt.Errorf("%v, %w, %v", err1, err2, err3)
41+
return fmt.Errorf("%w, %w, %w", err1, err2, err3)
3542
}
3643

3744
func ErrorStringFormat() error {

0 commit comments

Comments
 (0)