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

Fix a false positive for Style/RedundantParentheses #12505

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12505](https://github.com/rubocop/rubocop/pull/12505): Fix a false positive for `Style/RedundantParentheses` when using parenthesized `lambda` or `proc` with `do`...`end` block. ([@koic][])
4 changes: 3 additions & 1 deletion lib/rubocop/cop/style/redundant_parentheses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ def find_offense_message(begin_node, node)
return 'a literal' if disallowed_literal?(begin_node, node)
return 'a variable' if node.variable?
return 'a constant' if node.const_type?
return 'an expression' if node.lambda_or_proc?
if node.lambda_or_proc? && (node.braces? || node.send_node.lambda_literal?)
return 'an expression'
end
return 'an interpolated expression' if interpolation?(begin_node)

return if begin_node.chained?
Expand Down
31 changes: 31 additions & 0 deletions spec/rubocop/cop/style/redundant_parentheses_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,37 @@
RUBY
end

it 'registers an offense for parens around `->` with `do`...`end` block' do
expect_offense(<<~RUBY)
scope :my_scope, (-> do
^^^^^^ Don't use parentheses around an expression.
where(column: :value)
end)
RUBY

expect_correction(<<~RUBY)
scope :my_scope, -> do
where(column: :value)
end
RUBY
end

it 'does not register an offense for parens around `lambda` with `do`...`end` block' do
expect_no_offenses(<<~RUBY)
scope :my_scope, (lambda do
where(column: :value)
end)
RUBY
end

it 'does not register an offense for parens around `proc` with `do`...`end` block' do
expect_no_offenses(<<~RUBY)
scope :my_scope, (proc do
where(column: :value)
end)
RUBY
end

it_behaves_like 'plausible', '(-2)**2'
it_behaves_like 'plausible', '(-2.1)**2'

Expand Down