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

Enforce cuddling of error checks #65

Closed
JDiPierro opened this issue Jan 24, 2020 · 9 comments
Closed

Enforce cuddling of error checks #65

JDiPierro opened this issue Jan 24, 2020 · 9 comments

Comments

@JDiPierro
Copy link
Contributor

I'd love to be able to enforce that error checking is cuddled with the error assignment.

For example, right now this doesn't trigger the linter:

thing, err := db.GetThing()

if err != nil {
  return err
}

But I'd like it to :)

Thanks for the awesome linter!

@bombsimon
Copy link
Owner

Thanks for the issue!

Yeah that would be cool! I agree that the recommended way is to cuddle the error with the assignment in your example (and that's why it's allowed/supported).

I started working on fixing the errors and noticed that I had some design flaws I wanted to address first. I lost track a bit but I intend to pick up where I left and improve on this linter. When I do I'll add this feature as well!

If someone wants to create a PR in the meanwhile that's very welcomed!

@bombsimon
Copy link
Owner

Thanks to contributions from @JDiPierro this is now implemented and merged, thanks!

@JDiPierro Since you're the one who also implemented this I'll let you close the issue if/when you're ready!

@JDiPierro
Copy link
Contributor Author

I ran the error cuddling enforcement on a large codebase this morning and found a case that generates a false-positive:

if err = ProduceError(); err != nil {
	return err
}

I'll look into supporting that tonight. Other than that it ran great! Once that's fixed up I can put together a PR to golangci-lint with the new configuration option.

@bombsimon
Copy link
Owner

Ah, thanks! Should've seen that and also another example of bad test coverage. :( I thought I've written tests but I guess I'm pretty biased since I wrote the linter. 😄

Please do, no rush since nothing is tagged, see master as development.

@bombsimon
Copy link
Owner

@JDiPierro Have you tried out the master branch more this week? Do you think we can close this issue?

@JDiPierro
Copy link
Contributor Author

JDiPierro commented Feb 12, 2020

I haven't run it on any repos beside the one project I've already integrated wsl with. I'd expect it to throw many many errors on any other projects :P I can probably give it a shot though just to see if the errors it throws make sense. The repo that I have tested it with is 10k LoC according to SonarQube so it was a decent workout.

I was thinking of leaving this open until the golangci-lint changes get in for it and it's generally available. If you'd prefer to close it since the change is in master that's cool too :)

I have a start to the golanci-lint changes in the wsl__error_enforcement branch of the fork on my profile, here's the commit to add the new config: JDiPierro/golangci-lint@d37945a

But I believe before that can be merged WSL needs to cut a release so we can update the go.mod, vendor the new version, and then the new config would be valid. As it is now the wsl.Configuration is invalid because the new fields don't exist.

LMK how you'd like to proceed with that.

@bombsimon
Copy link
Owner

Alright, no worries, just curious! No rush, just wanted to know if everything seems OK. Feel free to leave this open!

I've drafted a new release (v2.1.0) on a new branch with the same name.

I don't really like the idea of basing releases of specific branches but I think this is alright for this time. I think I'll probably merge v2.1.0 into master and the next release will be built directly on master. It's just easier to keep track of everything that way.

@bombsimon
Copy link
Owner

PR in golangci-lint. Will close this issue when merged.

@bombsimon
Copy link
Owner

Merged to master in golangci-lint, will be in next release.

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

No branches or pull requests

2 participants