rubocop icon indicating copy to clipboard operation
rubocop copied to clipboard

Add a configuration option to NonLocalExitFromIterator to handle return values.

Open sloane opened this issue 1 year ago • 0 comments

Fixes #4064.

This solves a long-standing (August 2017) issue. An explicit return in a block has unintuitive behavior where the yield-ing code is skipped.

NonLocalExitFromIterator almost catches this error, it just needs a setting to make it more aggressive.

Example of the type of bug I'm trying to catch:

def execute_with_block
  result = yield
  puts "The result was #{result}"
  result
end

def handle_thing
  execute_with_block do 
    return 3
  end

foo = handle_thing # foo is 3, but the `puts` line never executes

Related: https://github.com/rubocop/rubocop/pull/12971

This is my first PR to Rubocop, so please let me know if I need additional work on this change.


Before submitting the PR make sure the following are checked:

  • [x] The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • [x] Wrote good commit messages.
  • [x] Commit message starts with [Fix #issue-number] (if the related issue exists).
  • [x] Feature branch is up-to-date with master (if not - rebase it).
  • [x] Squashed related commits together.
  • [x] Added tests.
  • [x] 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.

sloane avatar Aug 13 '24 20:08 sloane