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

new check: Err from initialization statement not used #1493

Open
jsageryd opened this issue Feb 5, 2024 · 1 comment
Open

new check: Err from initialization statement not used #1493

jsageryd opened this issue Feb 5, 2024 · 1 comment
Labels
aggressive A set of checks that is more prone to false positives but is helpful during code review new-check

Comments

@jsageryd
Copy link

jsageryd commented Feb 5, 2024

Instead of this:

if err := bar(); err != nil {
	log.Fatal(err)
}

I recently did this, using a previously declared err by mistake:

if bar(); err != nil {
	log.Fatal(err)
}

Is this something that staticcheck would be able to detect?

errcheck already in a way catches this case so arguably that could be enough, but errcheck (1) tends to report many "false positives" for errors that we might be ignoring on purpose, and (2) afaik only checks that the return value is assigned to something without caring about the context, missing something like (and this is a contrived example, but to illustrate):

if err2 := bar(); err != nil {
	log.Fatal(err2)
}

Full example: https://go.dev/play/p/0mhNf-1UKEw

@jsageryd jsageryd added the needs-triage Newly filed issue that needs triage label Feb 5, 2024
@dominikh
Copy link
Owner

dominikh commented Feb 5, 2024

At a minimum this would be a good fit for #1102. It might even make sense as a stricter check (that is, for the SA category), at least when we can prove that err hasn't escaped and that bar can't possibly modify its value.

@dominikh dominikh added new-check aggressive A set of checks that is more prone to false positives but is helpful during code review and removed needs-triage Newly filed issue that needs triage labels Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
aggressive A set of checks that is more prone to false positives but is helpful during code review new-check
Projects
None yet
Development

No branches or pull requests

2 participants