Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow %T verb to format errors #87

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion errorlint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func LintFmtErrorfCalls(fset *token.FileSet, info types.Info, multipleWraps bool
argIndex++
}

if verb.format == "w" {
if verb.format == "w" || verb.format == "T" {
continue
}
if argIndex-1 >= len(args) {
Expand Down
12 changes: 12 additions & 0 deletions errorlint/testdata/src/fmterrorf/fmterrorf.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ func NonWrappingVerb() error {
return fmt.Errorf("error: %v", err) // want "non-wrapping format verb for fmt.Errorf. Use `%w` to format errors"
}

func NonWrappingTVerb() error {
err := errors.New("oops")
return fmt.Errorf("error: %T", err)
}

func DoubleNonWrappingVerb() error {
err := errors.New("oops")
return fmt.Errorf("%v %v", err, err) // want "non-wrapping format verb for fmt.Errorf. Use `%w` to format errors"
Expand All @@ -27,6 +32,13 @@ func ErrorOneWrap() error {
return fmt.Errorf("%v, %w, %v", err1, err2, err3) // want "non-wrapping format verb for fmt.Errorf. Use `%w` to format errors"
}

func ValidNonWrappingTVerb() error {
err1 := errors.New("oops1")
err2 := errors.New("oops2")
err3 := errors.New("oops3")
return fmt.Errorf("%w, %T, %w", err1, err2, err3)
}

func ErrorMultipleWraps() error {
err1 := errors.New("oops1")
err2 := errors.New("oops2")
Expand Down
12 changes: 12 additions & 0 deletions errorlint/testdata/src/fmterrorf/fmterrorf.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ func NonWrappingVerb() error {
return fmt.Errorf("error: %w", err) // want "non-wrapping format verb for fmt.Errorf. Use `%w` to format errors"
}

func NonWrappingTVerb() error {
err := errors.New("oops")
return fmt.Errorf("error: %T", err)
}

func DoubleNonWrappingVerb() error {
err := errors.New("oops")
return fmt.Errorf("%w %w", err, err) // want "non-wrapping format verb for fmt.Errorf. Use `%w` to format errors"
Expand All @@ -27,6 +32,13 @@ func ErrorOneWrap() error {
return fmt.Errorf("%w, %w, %w", err1, err2, err3) // want "non-wrapping format verb for fmt.Errorf. Use `%w` to format errors"
}

func ValidNonWrappingTVerb() error {
err1 := errors.New("oops1")
err2 := errors.New("oops2")
err3 := errors.New("oops3")
return fmt.Errorf("%w, %T, %w", err1, err2, err3)
}

func ErrorMultipleWraps() error {
err1 := errors.New("oops1")
err2 := errors.New("oops2")
Expand Down