Skip to content

feat: Add automatic error comparison and type assertion fixes #96

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

Merged
merged 4 commits into from
Mar 22, 2025
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
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,24 @@ go-errorlint ./...
```
If there are one or more results, the exit status is set to `1`.

To automatically fix issues where possible, use the `-fix` flag:
```
go-errorlint -fix ./...
```

The tool can automatically fix:

> [!CAUTION]
> These fixes are still under development and the behavior is not yet stable.
> It is possible that it will make mistakes and cause more harm than good.
> Use with caution.

1. Non-wrapping format verb for fmt.Errorf (changing `%v` to `%w`)
2. Direct error comparisons (replacing `err == ErrFoo` with `errors.Is(err, ErrFoo)`)
3. Type assertions on errors (replacing `err.(*MyError)` with `errors.As` usage)

Complex cases like switches on errors or type switches cannot be automatically fixed.


## Examples

Expand Down Expand Up @@ -70,6 +88,8 @@ Errors returned from standard library functions that explicitly document that
an unwrapped error is returned are allowed by the linter. Notable cases are
`io.EOF` and `sql.ErrNoRows`.

You can pass `-fix` to have go-errorlint automatically fix these issues for you.

**Caveats**:
* Comparing the error returned from `(io.Reader).Read` to `io.EOF` without
`errors.Is` is considered valid as this is
Expand All @@ -95,6 +115,8 @@ var me MyError
ok := errors.As(err, &me)
```

You can pass `-fix` to have go-errorlint automatically fix these issues for you.

## Contributing

Do you think you have found a bug? Then please report it via the Github issue tracker. Make sure to
Expand Down
10 changes: 10 additions & 0 deletions errorlint/analysis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,13 @@ func TestIssueRegressions(t *testing.T) {
analyzer := NewAnalyzer()
analysistest.Run(t, analysistest.TestData(), analyzer, "issues")
}

func TestErrorComparisonFixes(t *testing.T) {
analyzer := NewAnalyzer()
analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), analyzer, "errorcompare")
}

func TestErrorTypeAssertionFixes(t *testing.T) {
analyzer := NewAnalyzer()
analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), analyzer, "errorassert")
}
Loading
Loading