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

Update Style/ReturnNilInPredicateMethodDefinition to recognize implicit nil returns in rescue and ensure #13125

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vlad-pisanov
Copy link
Contributor

@vlad-pisanov vlad-pisanov commented Aug 15, 2024

Style/ReturnNilInPredicateMethodDefinition already recognizes implicit nil returns from predicates:

def foo?
  nil # ⚠️ existing offense: Return false instead of nil in predicate methods
end

This PR expands implicit nil detection to predicates with rescue and ensure blocks. New cases detected:

def foo?
  nil # ⚠️ new offense: Return false instead of nil in predicate methods
rescue
end
def foo?
  nil # ⚠️ new offense: Return false instead of nil in predicate methods
ensure
end
def foo?
rescue
  nil # ⚠️ new offense: Return false instead of nil in predicate methods
end

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@Earlopain
Copy link
Contributor

I openend #13063 as a feature request for this cop but @koic felt like it would be more appropriate as a new cop instead. I'm not sure of the name (maybe just ImplicitReturnNilInPredicateMethodDefinition?) but conceptually this looks like a good start.

ensure
bar
nil
^^^ Return `false` instead of `nil` in predicate methods.
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't believe that ensure will ever return a value without an explicit return. This method would return nil from the empty block above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch! I'm surprised Lint/Void doesn't pick up on this, sounds like a false negative 👀

Copy link
Collaborator

Choose a reason for hiding this comment

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

Indeed! We need to improve Lint/Void a bit. :-)

@vlad-pisanov
Copy link
Contributor Author

@Earlopain I'm with you -- since this cop already detects the basic implicit nil return scenario, it makes sense to me to expand this cop to handle more implicit return scenarios.

…icit `nil` returns in `rescue` and `ensure`
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

Successfully merging this pull request may close these issues.

3 participants