Skip to content

Check all levels of embedded interfaces against the exclude list.#137

Merged
kisielk merged 7 commits into
kisielk:masterfrom
louissobel:add-receiver-checking-against-exclude
May 22, 2018
Merged

Check all levels of embedded interfaces against the exclude list.#137
kisielk merged 7 commits into
kisielk:masterfrom
louissobel:add-receiver-checking-against-exclude

Conversation

@louissobel
Copy link
Copy Markdown
Contributor

This addresses #132 .

When generating names of a call to check against the exclusion list, errcheck currently just uses the name of the function itself. For example, for the Write call in

var h hash.Hash
h.Write([]byte{})

the checked name will be (io.Writer).Write. This limits the usefulness of the exclusion list, because we'd really like to be able to exclude (hash.Hash).Write, which is documented to never return an error.

So this PR adds code (embedded_walker.go) to search through a types.Selection of a types.Func, recording all the interfaces that are passed through while descending down to the actual declaration of that Func. In the example above, we'd observe that we pass through two interfaces getting to Write: hash.Hash and io.Writer. It also deals the receiver being a struct with an embedded interface.

Then we use this code in errcheck to generate multiple names to check against the exclusion list: ["(hash.Hash).Write", "(io.Writer).Write"] in this example.

And finally, the PR adds (hash.Hash).Write to the default exclusion list as a use case for all this code.

@louissobel louissobel force-pushed the add-receiver-checking-against-exclude branch 4 times, most recently from 077a089 to 5584f04 Compare April 8, 2018 19:25
Copy link
Copy Markdown
Owner

@kisielk kisielk left a comment

Choose a reason for hiding this comment

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

Looks good. A few additional comments would be helpful, especially 6 months down the road when everyone forgets what all this code does :)

}

func (v *visitor) fullName(call *ast.CallExpr) (string, bool) {
func (v *visitor) selectorAndFunc(call *ast.CallExpr) (*ast.SelectorExpr, *types.Func, bool) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

While you're here, can you add a comment to this function, particularly the return values?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

👍

Comment thread internal/errcheck/errcheck.go Outdated

}

func (v *visitor) fullName(call *ast.CallExpr) (string, bool) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Also doc this function

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

👍

return fn.FullName(), true
}

func (v *visitor) namesForExcludeCheck(call *ast.CallExpr) []string {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

And doc this one as well

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

👍

return s.Field(fieldIndex).Type()
}

func getEmbeddedInterfaceDefiningMethod(interfaceT *types.Interface, fn *types.Func) (*types.Named, bool) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Can you please document the return values of this one?

The rest of the function returns in this file are pretty self explanatory but this one may be confusing down the road.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

👍

@kisielk
Copy link
Copy Markdown
Owner

kisielk commented May 17, 2018

Ping @louissobel

I'm basically ready to merge this after the docs are added :)

@louissobel louissobel force-pushed the add-receiver-checking-against-exclude branch from 5584f04 to cd18fe7 Compare May 19, 2018 15:44
@louissobel
Copy link
Copy Markdown
Contributor Author

thanks for the nudge!

docs added

@kisielk kisielk merged commit 55d8f50 into kisielk:master May 22, 2018
@kisielk
Copy link
Copy Markdown
Owner

kisielk commented May 22, 2018

Great, thanks a lot !

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.

2 participants