-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Implement NoReturnFromBlock
rule
#12971
base: master
Are you sure you want to change the base?
Implement NoReturnFromBlock
rule
#12971
Conversation
NoReturnFromBlock
rule
One issue that I see which such a rule is that it's more or less the same as forbidding the use of procs, as the behavior of @rubocop/rubocop-core What do you think this PR? |
module Cop | ||
module Lint | ||
# Checks for blocks that have a return statement. | ||
# Return statements in blocks are typically an oversight that can lead to bugs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be good to clarify about the nature of the problems that can be caused.
private | ||
|
||
def allow_return_from_block? | ||
cop_config['AllowReturnFromBlock'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the point of this config, as it's essentially the same as disabling the cop. Also - what about procs? They are essentially blocks for all practical purposes.
The bad case described in the example of this PR can already be detected by # @example
# # bad
# items.each do |item|
# return if item.nil?
# puts item.some_attribute
# end https://docs.rubocop.org/rubocop/1.64/cops_lint.html#lintnonlocalexitfromiterator |
Ah, great point! I had forgotten about it. |
Ah I wasn't aware of this one, let me test that rule out and see if why it didn't catch the bugs we had recently, I'll close this PR if it is indeed redundant. |
@koic @bbatsov how about return with an expression, which is allowed by items.each do |item|
return nil if item.nil? # Lint/NonLocalExitFromIterator doesn't complain about this
# or
return 'something' if item.nil? # this is also okay
# ...
end |
Fixes #4064.
This PR adds a
NoReturnFromBlock
rule that warns whenreturn
is called from inside a block. This rule is turned off by default so as not to cause any breaking changes. It can be enabled by settingAllowReturnFromBlock
tofalse
in the config.This rule is important as a
return
in a block can often cause bugs.Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.