Skip to content
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

Feature/ignore interface regex #25

Merged

Conversation

guillaumeio
Copy link
Contributor

Hi Tom!
This PR intends to bring flexibility to wrapcheck with errors returned from Interfaces types:

Allows ignore unwrapped errors returned from interfaces. Define regexps rules to explicitly ignore underlying interfaces with mandated methods to ignore, according to the interface names. Even though this could technically be done with thought-out ignoreSigRegexps, this allows to explicitly and cleanly define rules in the config to define sub cases of IsInterface.

PS: fixed typos

added ignoreInterfaceRegexps option to ignore errors returned from a function call defined on a interface
Copy link
Owner

@tomarrell tomarrell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Guillaume!

Thanks a lot for the PR, much appreciated. I've left a few minor comments.

Cheers for adding the test as well.

var str string
err := fn.Decode(&str)
if err != nil {
return err // errorer interface ignored
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor suggestion

Suggested change
return err // errorer interface ignored
return err // errorer interface ignored as per `ignoreInterfaceRegexps`

README.md Outdated
@@ -52,6 +52,13 @@ ignoreSigRegexps:
ignorePackageGlobs:
- encoding/*
- github.com/pkg/*

# An array of strings which specify regular expressions of interface names.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could make this a little more succinct. I liked the description on the config struct more, maybe we could use that here.

// IgnoreInterfaceRegexps defines a list of regular expressions which, if matched
// to a underlying interface name, will ignore unwrapped errors returned from a
// function whose call is defined on the given interface.

@@ -91,11 +101,15 @@ func NewAnalyzer(cfg WrapcheckConfig) *analysis.Analyzer {

func run(cfg WrapcheckConfig) func(*analysis.Pass) (interface{}, error) {
// Precompile the regexps, report the error
ignoreSigRegexp, err := compileRegexps(cfg.IgnoreSigRegexps)
ignoreSigRegexp, err1 := compileRegexps(cfg.IgnoreSigRegexps)
ignoreInterfaceRegexps, err2 := compileRegexps(cfg.IgnoreInterfaceRegexps)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking, I've actually made some similar changes, still local for now, in order to precompile the globs. Could we maybe update this to something along the lines of:

var (
	ignoreSigRegexp    []*regexp.Regexp
	ignorePackageGlobs []glob.Glob
	err                error
)

ignoreSigRegexp, err = compileRegexps(cfg.IgnoreSigRegexps)
if err != nil {
	ignorePackageGlobs, err = compileGlobs(cfg.IgnorePackageGlobs)
}

I will likely refactor this anyway in an upcoming PR, but for now I'd prefer not to have multiple error checks in the returned func below.

return
}

// Check if the underlying type of the "x" in x.y.z is an interface, as
// errors returned from interface types should be wrapped.
if isInterface(pass, sel) {
pass.Reportf(tokenPos, "error returned from interface method should be wrapped: sig: %s", fnSig)
return
if containsMatch(regexpsInter, types.TypeString(pass.TypesInfo.TypeOf(sel.X), func(p *types.Package) string { return p.Name() })) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we maybe pull this out of a single line? It's a little dense to read as it is and could be simpler.

@guillaumeio
Copy link
Contributor Author

Thanks for the review!
Tried my go at refactoring. Please let me know if this is what you intended :)

Copy link
Owner

@tomarrell tomarrell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job! Thanks a lot for that. Didn't actually intend for you to also add the globs, but I appreciate you doing it!

Will merge this now and probably cut a new release tomorrow.

Thanks again for the contribution!

@tomarrell tomarrell merged commit 624a3b4 into tomarrell:master Mar 23, 2022
@guillaumeio
Copy link
Contributor Author

Thanks for the kind words!
Would love a new release :)
Cheers Tom!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants