Skip to content

Repository files navigation

lesiw.io/errfmt

Go Reference CI Release Go Version Discord License

An analysis.Analyzer for error format strings: error construction routes through fmt.Errorf, and error wrapping routes through its %w verb.

errors.New can neither format nor wrap; fmt.Errorf does both, is imported nearly everywhere already, and one error constructor is simpler than two.

Checks

errors.New can be replaced by fmt.Errorf

errors.New("connection lost") // errors.New can be replaced by fmt.Errorf

Reported only: a message could contain a percent sign Errorf would interpret, so the rewrite is the author's work.

Errors wrap with %w

An error value formatted into another error can be wrapped: %v and %s verbs with error arguments become %w.

fmt.Errorf("open failed: %v", err) // error can be wrapped with %w, not %v

The fix rewrites the verb in place, and is withheld when the format's source spelling differs from its value, as with escape sequences. Only arguments whose static type is error are considered, and only literal format strings are inspected. An indexed or star verb makes the argument mapping non-positional and skips the whole call.

Usage

go get -tool lesiw.io/errfmt/cmd/errfmt
go tool errfmt ./...

About

An analysis.Analyzer for error format strings: construction through fmt.Errorf, wrapping through %w.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages