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

Add one more check in gocriticNilValReturn #100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ func gocriticWrapperFunc(m fluent.Matcher) {
func gocriticNilValReturn(m fluent.Matcher) {
m.Match(`if $*_; $v == nil { return $v }`).
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we drop this one? or combine them into one? @quasilyte

Copy link
Owner

@quasilyte quasilyte Oct 26, 2020

Choose a reason for hiding this comment

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

I thought that this pattern catches both if statements with and without the init clause. I'll re-check this with gogrep.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Buuuuuut we can make two rules with suggestion. Not one with some info and one with a fix.

Copy link
Owner

Choose a reason for hiding this comment

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

That's correct. But the form with the fix should go before this one.
Otherwise, the more generic pattern will match and we'll never get a suggestion.
At least it looks like we need to move the new rule above this one.

Report(`returned expr is always nil; replace $v with nil`)
m.Match(`if $v == nil { return $v }`).
Suggest(`if $v == nil { return nil }`)
}

func gocriticBoolExprSimplify(m fluent.Matcher) {
Expand Down