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

Checker for defered functions that return a function #767

Closed
arp242 opened this issue May 31, 2020 · 2 comments
Closed

Checker for defered functions that return a function #767

arp242 opened this issue May 31, 2020 · 2 comments

Comments

@arp242
Copy link
Contributor

arp242 commented May 31, 2020

The below code will print nothing since f() returns a function, you need to use defer f()() if you want it to actually run it. I think it would be useful to add a linter for this, since you practically never not want to run the returned function.

func main() {
    defer f()
}

func f() func() {
    return func() { fmt.Println("x") }
}

The Go compiler already refuses to compile just defer f, so perhaps it might be better to expand that check instead of adding one to staticcheck?

@arp242 arp242 added the needs-triage Newly filed issue that needs triage label May 31, 2020
@arp242 arp242 changed the title Functions calls that are not run Checker for defered functions that return a function May 31, 2020
@arp242
Copy link
Contributor Author

arp242 commented May 31, 2020

Added a basic implementation here: arp242/goatcounter@45a63f0#diff-a5bff9fc0a1ecd49d6c47a6a7a12b468

This would have prevented a serious bug at least in one case btw, which unfortunately wasn't covered by the integration tests :-(

@dominikh
Copy link
Owner

dominikh commented Jun 1, 2020

Closing as a duplicate of #466.

The Go compiler already refuses to compile just defer f, so perhaps it might be better to expand that check instead of adding one to staticcheck?

The compiler can't refuse to compile this code. f might do other meaningful work, and the language specification doesn't forbid defers of functions that return functions.

@dominikh dominikh closed this as completed Jun 1, 2020
@dominikh dominikh added duplicate new-check and removed needs-triage Newly filed issue that needs triage labels Jun 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants