Skip to content

Add redundant-test-main-exit rule #1208

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

Merged
merged 5 commits into from
Jan 29, 2025

Conversation

semihbkgr
Copy link
Contributor

The redundant-test-main-exit rule reports unnecessary Exit calls in TestMain functions, as the Go test runner has handled termination automatically since Go 1.15; the details are explained in the issue.

fixes #1189

The deep-exit rule currently excludes reporting Exit calls in TestMain functions, but we could consider updating its behavior to include these calls, which might eliminate the need to define this new rule.

Copy link
Collaborator

@chavacava chavacava left a comment

Choose a reason for hiding this comment

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

Hi @semihbkgr thank you for tha PR.

I' ve left some comments.
One more thing, we should only apply the rule if the code under analysis is on a Go version 1.15 or higher. To do so, you can take inspiration in other rules like range-val-in_closure

if file.Pkg.IsAtLeastGo122() {

(that should also be tested)

Thanks again


pkg := id.Name
fn := fc.Sel.Name
if isCallToExitFunction(pkg, fn) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

isCallToExitFunction will return true even for function calls other than os.Exit (for example log.Fatal, log.Panic ...) That behavior is okay for what we check here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @chavacava, thank you for your detailed review and suggestions!

I’m not entirely sure about including logging functions other than os.Exit or syscall.Exit. It’s uncommon (in my experience) to call logging functions that internally invoke Exit, but I don't see any downside to including them in this check.

What’s your opinion on this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The issue with including logging functions is that the common failure message may not be appropriate for them, as these functions are not necessarily redundant: they first log a message and then exit.

@semihbkgr semihbkgr requested a review from chavacava January 28, 2025 19:44
@semihbkgr semihbkgr requested a review from alexandear January 28, 2025 20:45
@chavacava chavacava merged commit 5f01efa into mgechev:master Jan 29, 2025
5 checks passed
@chavacava
Copy link
Collaborator

Merged! Thank you @semihbkgr (and @alexandear)

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.

Rule for detecting useless os.Exit in TestMain
3 participants