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

Style/RedundantReturn removes non-redundant returns from begin-rescue-else-end blocks #8767

Closed
betesh opened this issue Sep 22, 2020 · 0 comments · Fixed by #8768
Closed

Comments

@betesh
Copy link
Contributor

betesh commented Sep 22, 2020

When the begin block of a begin-rescue-else-end contains an explicit return, the else block is not executed. Removing that return causes the else block to be executed. Thus, the return is not redundant. However, rubocop flags it as redundant.


Expected behavior

Rubocop should not consider the return redundant, since removing it alters behavior

Actual behavior

Rubocop considers the return to be redundant, and removes it when the --auto-correct flag is supplied

Steps to reproduce the problem

$ cat test.rb
# frozen_string_literal: true

def try_to_return_true
  return true
rescue NoMethodError
  puts 'I can only return false'
  false
else
  puts 'I thought I already returned true'
  5
end

puts try_to_return_true
$ ruby test.rb 
true
$ rubocop --auto-correct test.rb 
Inspecting 1 file
C

Offenses:

test.rb:4:3: C: [Corrected] Style/RedundantReturn: Redundant return detected.
  return true
  ^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected
$ ruby test.rb 
I thought I already returned true
5

RuboCop version

$ rubocop -V
0.91.0 (using Parser 2.7.1.4, rubocop-ast 0.4.2, running on ruby 2.6.0 x86_64-linux)
@betesh betesh changed the title RedundantReturn removes non-redundant returns from begin-rescue-else blocks Style/RedundantReturn removes non-redundant returns from begin-rescue-else-end blocks Sep 22, 2020
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 a pull request may close this issue.

1 participant