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

Capture yet more bad defer / recover patterns #719

Merged
merged 5 commits into from Jul 24, 2022

Conversation

Groxx
Copy link
Contributor

@Groxx Groxx commented Jul 23, 2022

defer + recover() is an eternal font of excitement.

This adds a new category under defer, "immediate-recover", which warns about cases like these:

defer recover()
defer helper(recover())

because they do not do what they appear to do. Neither will recover a panic (in normal use).

Tests and lint-tests include full demonstrations of ^ those, as well as some fairly exotic example where those do work.
I have not ever seen these in practice, so for the moment I've set the confidence level to 1 - IMO both of these are very strong signals of flawed code, and worth running by default.

I'm fairly sure I'm missing some things from this PR, e.g. documentation.
I'll try to look tomorrow, but I'd be happy for any pointers :) I'm happy to flesh it out.

Closes #718, which also contains motivation and other details.

`recover()` is an eternal font of excitement.
@Groxx Groxx changed the title Yet more bad defer / recover patterns Capture yet more bad defer / recover patterns Jul 23, 2022
@Groxx Groxx marked this pull request as ready for review July 23, 2022 08:02
@@ -23,3 +23,82 @@ func TestDeferOthersDisabled(t *testing.T) {
Arguments: []interface{}{[]interface{}{"loop"}},
})
}

func TestDemonstrateIneffectiveDefers(t *testing.T) {
Copy link
Collaborator

@chavacava chavacava Jul 23, 2022

Choose a reason for hiding this comment

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

Usually this kind of tests is not part of rules tests. You could add all these cases to testdata/defer.go

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yea, my goal was to provide executable evidence that the patterns being detected behave the way the code / warnings describe. these aren't linted, they're run as part of the test suite.

I'm entirely fine just removing them if you'd prefer to keep the tests more focused on running the linter or something, I wrote them half to re-prove it to myself :)

@chavacava
Copy link
Collaborator

Hi @Groxx thanks for the PR, I've left one comment on it.
If you are motivated enough, you could enrich the documentation of the rule at [RULES_DESCRIPTIONS.md](https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md) with some detail on why those ways of using defer are not ok

@chavacava chavacava merged commit db56db0 into mgechev:master Jul 24, 2022
@chavacava
Copy link
Collaborator

Thanks @Groxx !

ZackLK added a commit to uber/cadence that referenced this pull request Jul 26, 2022
ZackLK added a commit to uber/cadence that referenced this pull request Jul 26, 2022
ZackLK added a commit to uber/cadence that referenced this pull request Jul 26, 2022
* Update to latest version of revive that includes Steven's change to find more defer/recover badness.

mgechev/revive#719

* Ignore 158 new warnings about package-comments.

* Add Steven's new immediate-recover linter.
- Verified this would have caught the previous mistakes I made (woohoo!)
ZackLK added a commit to uber/cadence that referenced this pull request Aug 22, 2022
* Update to latest version of revive that includes Steven's change to find more defer/recover badness.

mgechev/revive#719

* Ignore 158 new warnings about package-comments.

* Add Steven's new immediate-recover linter.
- Verified this would have caught the previous mistakes I made (woohoo!)
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.

Capture more bad / risky defer+recover patterns
2 participants