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

Commit 5095374

Browse files
kakkoyunpolyfloyd
authored andcommitted
feat: add option helpers for comparison and assertion checks
Add WithComparison and WithAsserts option helpers to allow programmatic configuration of checkComparison and checkAsserts flags. This enables fine-grained control over which error linting rules are applied when using the analyzer as a library. - Add WithComparison(bool) option helper for comparison checking - Add WithAsserts(bool) option helper for assertion checking - Add corresponding test cases and test data - All tests pass Signed-off-by: Kemal Akkoyun <[email protected]>
1 parent d9513b1 commit 5095374

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

errorlint/options.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,15 @@ func WithAllowedWildcard(ap []AllowPair) Option {
1313
allowedWildcardAppend(ap)
1414
}
1515
}
16+
17+
func WithComparison(enabled bool) Option {
18+
return func() {
19+
checkComparison = enabled
20+
}
21+
}
22+
23+
func WithAsserts(enabled bool) Option {
24+
return func() {
25+
checkAsserts = enabled
26+
}
27+
}

errorlint/options_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ func TestOption(t *testing.T) {
2727
}),
2828
pattern: "options/withAllowedWildcard",
2929
},
30+
{
31+
desc: "WithComparison",
32+
opt: errorlint.WithComparison(true),
33+
pattern: "options/withComparison",
34+
},
35+
{
36+
desc: "WithAsserts",
37+
opt: errorlint.WithAsserts(true),
38+
pattern: "options/withAsserts",
39+
},
3040
}
3141

3242
for _, tt := range testCases {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package testdata
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type MyError struct{}
8+
9+
func (*MyError) Error() string {
10+
return "my custom error"
11+
}
12+
13+
func doSomething() error {
14+
return &MyError{}
15+
}
16+
17+
// This should be flagged when assert checking is enabled
18+
func TypeAssertionDirect() {
19+
err := doSomething()
20+
me, ok := err.(*MyError) // want "type assertion on error will fail on wrapped errors. Use errors.As to check for specific errors"
21+
if ok {
22+
fmt.Println("got my error:", me)
23+
}
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package testdata
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
var ErrSentinel = errors.New("sentinel error")
9+
10+
func doSomething() error {
11+
return ErrSentinel
12+
}
13+
14+
// This should be flagged when comparison checking is enabled
15+
func CompareWithEquals() {
16+
err := doSomething()
17+
if err == ErrSentinel { // want "comparing with == will fail on wrapped errors. Use errors.Is to check for a specific error"
18+
fmt.Println("sentinel error")
19+
}
20+
}

0 commit comments

Comments
 (0)