You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
When the
begin
block of abegin-rescue-else-end
contains an explicitreturn
, theelse
block is not executed. Removing thatreturn
causes theelse
block to be executed. Thus, thereturn
is not redundant. However, rubocop flags it as redundant.Expected behavior
Rubocop should not consider the
return
redundant, since removing it alters behaviorActual behavior
Rubocop considers the
return
to be redundant, and removes it when the--auto-correct
flag is suppliedSteps to reproduce the problem
RuboCop version
The text was updated successfully, but these errors were encountered: