Skip to content

Commit

Permalink
Merge pull request #12395 from koic/fix_false_negatives_for_style_red…
Browse files Browse the repository at this point in the history
…undant_return

[Fix #12394] Fix false negatives for `Style/RedundantReturn`
  • Loading branch information
koic committed Nov 16, 2023
2 parents 37fe32d + c780552 commit 5ccf58a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12394](https://github.com/rubocop/rubocop/issues/12394): Fix false negatives for `Style/RedundantReturn` when `lambda`, `->`, or `proc` ending with `return`. ([@koic][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/redundant_return.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class RedundantReturn < Base

MSG = 'Redundant `return` detected.'
MULTI_RETURN_MSG = 'To return multiple values, use an array.'
RESTRICT_ON_SEND = %i[define_method define_singleton_method].freeze
RESTRICT_ON_SEND = %i[define_method define_singleton_method lambda proc].freeze

def on_send(node)
return unless (parent = node.parent) && parent.block_type?
Expand Down
51 changes: 51 additions & 0 deletions spec/rubocop/cop/style/redundant_return_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,57 @@ def func
RUBY
end

it 'reports an offense for lambda ending with return' do
expect_offense(<<~RUBY)
lambda do
some_preceding_statements
return something
^^^^^^ Redundant `return` detected.
end
RUBY

expect_correction(<<~RUBY)
lambda do
some_preceding_statements
something
end
RUBY
end

it 'reports an offense for -> ending with return' do
expect_offense(<<~RUBY)
-> do
some_preceding_statements
return something
^^^^^^ Redundant `return` detected.
end
RUBY

expect_correction(<<~RUBY)
-> do
some_preceding_statements
something
end
RUBY
end

it 'reports an offense for proc ending with return' do
expect_offense(<<~RUBY)
proc do
some_preceding_statements
return something
^^^^^^ Redundant `return` detected.
end
RUBY

expect_correction(<<~RUBY)
proc do
some_preceding_statements
something
end
RUBY
end

it 'reports an offense for def ending with return with splat argument' do
expect_offense(<<~RUBY)
def func
Expand Down

0 comments on commit 5ccf58a

Please sign in to comment.