diff --git a/errorlint/options.go b/errorlint/options.go index 4d7c742..64d4352 100644 --- a/errorlint/options.go +++ b/errorlint/options.go @@ -13,3 +13,15 @@ func WithAllowedWildcard(ap []AllowPair) Option { allowedWildcardAppend(ap) } } + +func WithComparison(enabled bool) Option { + return func() { + checkComparison = enabled + } +} + +func WithAsserts(enabled bool) Option { + return func() { + checkAsserts = enabled + } +} diff --git a/errorlint/options_test.go b/errorlint/options_test.go index 9005911..88a4ccc 100644 --- a/errorlint/options_test.go +++ b/errorlint/options_test.go @@ -27,6 +27,16 @@ func TestOption(t *testing.T) { }), pattern: "options/withAllowedWildcard", }, + { + desc: "WithComparison", + opt: errorlint.WithComparison(true), + pattern: "options/withComparison", + }, + { + desc: "WithAsserts", + opt: errorlint.WithAsserts(true), + pattern: "options/withAsserts", + }, } for _, tt := range testCases { diff --git a/errorlint/testdata/src/options/withAsserts/withAsserts.go b/errorlint/testdata/src/options/withAsserts/withAsserts.go new file mode 100644 index 0000000..08648d7 --- /dev/null +++ b/errorlint/testdata/src/options/withAsserts/withAsserts.go @@ -0,0 +1,24 @@ +package testdata + +import ( + "fmt" +) + +type MyError struct{} + +func (*MyError) Error() string { + return "my custom error" +} + +func doSomething() error { + return &MyError{} +} + +// This should be flagged when assert checking is enabled +func TypeAssertionDirect() { + err := doSomething() + me, ok := err.(*MyError) // want "type assertion on error will fail on wrapped errors. Use errors.As to check for specific errors" + if ok { + fmt.Println("got my error:", me) + } +} \ No newline at end of file diff --git a/errorlint/testdata/src/options/withComparison/withComparison.go b/errorlint/testdata/src/options/withComparison/withComparison.go new file mode 100644 index 0000000..841a3bd --- /dev/null +++ b/errorlint/testdata/src/options/withComparison/withComparison.go @@ -0,0 +1,20 @@ +package testdata + +import ( + "errors" + "fmt" +) + +var ErrSentinel = errors.New("sentinel error") + +func doSomething() error { + return ErrSentinel +} + +// This should be flagged when comparison checking is enabled +func CompareWithEquals() { + err := doSomething() + if err == ErrSentinel { // want "comparing with == will fail on wrapped errors. Use errors.Is to check for a specific error" + fmt.Println("sentinel error") + } +} \ No newline at end of file