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

Validate error if blocks #78

Closed
artruora opened this issue Apr 17, 2020 · 4 comments · Fixed by #79
Closed

Validate error if blocks #78

artruora opened this issue Apr 17, 2020 · 4 comments · Fixed by #79
Labels
golangci-only Fixed in wsl but not merged to golangci-lint

Comments

@artruora
Copy link

Hi, I have the following issue while using the wsl tool

When two "if" blocks are together, I get: "if statements should only be cuddled with assignments"

func validateErr(err error) {
	if err == nil {
         ...
	}
	if errors.Is(err, ErrSomething) {
         ...
	}
...
}

But if I insert a space between them, i get : "if statements that check an error must be cuddled with the statement that assigned the error"

func validateErr(err error) {
	if err == nil {
         ...
	}

	if errors.Is(err, ErrSomething) {
         ...
	}
...
}
@bombsimon
Copy link
Owner

Thank your for the issue! That was interesting, haven't seen this yet but I guess it's kind of new since errors.Is() introduced.

So just to clarify, this only happens when ForceCuddleErrCheckAndAssign is set to true and ErrorVariableNames is set to err (this is default when using -force-err-cuddling).

I think I'll fix this by checking that if this is enabled, it will only trigger an error if the previous statement is an assign expression because it's only those we're looking to merge, assignments and checks.

@bombsimon
Copy link
Owner

bombsimon commented Apr 17, 2020

This is now fixed and released under v3.1.0, please give it a try!

If you're using golangci-lint this change will not be reflected until an updated version is merged to master. In the meantime you can build your own version golangci-lint by cloning the repository and update the dependencies.

$ cd golangci-lint
$ go get -u github.com/bombsimon/wsl/v3@v3.1.0
$ go install ./...

@bombsimon
Copy link
Owner

Actually, I'll leave this open until it's merged to golangci-lint so I don't forget tis. :)

@bombsimon bombsimon reopened this Apr 17, 2020
@bombsimon bombsimon added the golangci-only Fixed in wsl but not merged to golangci-lint label May 8, 2020
@bombsimon
Copy link
Owner

Merged in golangci/golangci-lint#1750, will be available in next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
golangci-only Fixed in wsl but not merged to golangci-lint
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants