Skip to content
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
12 changes: 12 additions & 0 deletions errorlint/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
10 changes: 10 additions & 0 deletions errorlint/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions errorlint/testdata/src/options/withAsserts/withAsserts.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
20 changes: 20 additions & 0 deletions errorlint/testdata/src/options/withComparison/withComparison.go
Original file line number Diff line number Diff line change
@@ -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")
}
}