Skip to content

Commit 040cb97

Browse files
authored
feat: Add automatic error comparison and type assertion fixes (#96)
Signed-off-by: Kemal Akkoyun <[email protected]> Co-authored-by: ccoVeille <[email protected]> Enhance errorlint analyzer with automatic fixes for: - Error comparisons using `==` and `!=` - Type assertions on errors - Error switches Adds suggested fixes to: - Replace direct error comparisons with `errors.Is()` - Convert type assertions to use `errors.As()` - Transform error switches to use `errors.Is()` and `errors.As()` Includes new test cases and golden files to validate the new automatic fixing capabilities. * feat: Improve errorlint with better variable naming and error handling - Add function for meaningful error variable names - Preserve original "ok" variable names in type assertions - Use errors.As() with generated variable names - Add tests for custom and wrapped errors
1 parent aa9b5f9 commit 040cb97

File tree

7 files changed

+1368
-16
lines changed

7 files changed

+1368
-16
lines changed

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ go-errorlint ./...
2424
```
2525
If there are one or more results, the exit status is set to `1`.
2626

27+
To automatically fix issues where possible, use the `-fix` flag:
28+
```
29+
go-errorlint -fix ./...
30+
```
31+
32+
The tool can automatically fix:
33+
34+
> [!CAUTION]
35+
> These fixes are still under development and the behavior is not yet stable.
36+
> It is possible that it will make mistakes and cause more harm than good.
37+
> Use with caution.
38+
39+
1. Non-wrapping format verb for fmt.Errorf (changing `%v` to `%w`)
40+
2. Direct error comparisons (replacing `err == ErrFoo` with `errors.Is(err, ErrFoo)`)
41+
3. Type assertions on errors (replacing `err.(*MyError)` with `errors.As` usage)
42+
43+
Complex cases like switches on errors or type switches cannot be automatically fixed.
44+
2745

2846
## Examples
2947

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

91+
You can pass `-fix` to have go-errorlint automatically fix these issues for you.
92+
7393
**Caveats**:
7494
* Comparing the error returned from `(io.Reader).Read` to `io.EOF` without
7595
`errors.Is` is considered valid as this is
@@ -95,6 +115,8 @@ var me MyError
95115
ok := errors.As(err, &me)
96116
```
97117

118+
You can pass `-fix` to have go-errorlint automatically fix these issues for you.
119+
98120
## Contributing
99121

100122
Do you think you have found a bug? Then please report it via the Github issue tracker. Make sure to

errorlint/analysis_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,13 @@ func TestIssueRegressions(t *testing.T) {
4343
analyzer := NewAnalyzer()
4444
analysistest.Run(t, analysistest.TestData(), analyzer, "issues")
4545
}
46+
47+
func TestErrorComparisonFixes(t *testing.T) {
48+
analyzer := NewAnalyzer()
49+
analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), analyzer, "errorcompare")
50+
}
51+
52+
func TestErrorTypeAssertionFixes(t *testing.T) {
53+
analyzer := NewAnalyzer()
54+
analysistest.RunWithSuggestedFixes(t, analysistest.TestData(), analyzer, "errorassert")
55+
}

0 commit comments

Comments
 (0)