Skip to content

Commit

Permalink
[Fix #8299] Fix an incorrect auto-correct for Style/RedundantCondition
Browse files Browse the repository at this point in the history
Fixes #8299

This PR fixes an incorrect auto-correct for `Style/RedundantCondition`
when using `raise`, `rescue`, or `and` without argument parentheses in `else`.
  • Loading branch information
koic authored and bbatsov committed Jul 14, 2020
1 parent 81c39c9 commit 672bac8
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

* [#8324](https://github.com/rubocop-hq/rubocop/issues/8324): Fix crash for `Layout/SpaceAroundMethodCallOperator` when using `Proc#call` shorthand syntax. ([@fatkodima][])
* [#8323](https://github.com/rubocop-hq/rubocop/issues/8323): Fix a false positive for `Style/HashAsLastArrayItem` when hash is not a last array item. ([@fatkodima][])
* [#8299](https://github.com/rubocop-hq/rubocop/issues/8299): Fix an incorrect auto-correct for `Style/RedundantCondition` when using `raise`, `rescue`, or `and` without argument parentheses in `else`. ([@koic][])

## 0.88.0 (2020-07-13)

Expand Down
18 changes: 15 additions & 3 deletions lib/rubocop/cop/style/redundant_condition.rb
Expand Up @@ -90,10 +90,10 @@ def use_if_branch?(else_branch)
end

def else_source(else_branch)
if else_branch.basic_conditional? &&
else_branch.modifier_form? ||
else_branch.range_type?
if require_parentheses?(else_branch)
"(#{else_branch.source})"
elsif without_argument_parentheses_method?(else_branch)
"#{else_branch.method_name}(#{else_branch.arguments.map(&:source).join(', ')})"
else
else_branch.source
end
Expand All @@ -118,6 +118,18 @@ def correct_ternary(corrector, node)

corrector.wrap(node.else_branch, '(', ')')
end

def require_parentheses?(node)
node.basic_conditional? &&
node.modifier_form? ||
node.range_type? ||
node.rescue_type? ||
node.respond_to?(:semantic_operator?) && node.semantic_operator?
end

def without_argument_parentheses_method?(node)
node.send_type? && !node.arguments.empty? && !node.parenthesized?
end
end
end
end
Expand Down
60 changes: 60 additions & 0 deletions spec/rubocop/cop/style/redundant_condition_spec.rb
Expand Up @@ -42,6 +42,36 @@
RUBY
end

it 'registers an offense and corrects when `raise` without argument parentheses in `else`' do
expect_offense(<<~RUBY)
if b
^^^^ Use double pipes `||` instead.
b
else
raise 'foo'
end
RUBY

expect_correction(<<~RUBY)
b || raise('foo')
RUBY
end

it 'registers an offense and corrects when a method without argument parentheses in `else`' do
expect_offense(<<~RUBY)
if b
^^^^ Use double pipes `||` instead.
b
else
do_something foo, bar, key: :value
end
RUBY

expect_correction(<<~RUBY)
b || do_something(foo, bar, key: :value)
RUBY
end

it 'registers an offense and corrects complex one liners' do
expect_offense(<<~RUBY)
if b
Expand Down Expand Up @@ -252,6 +282,36 @@
time_period = updated_during || (2.days.ago...Time.now)
RUBY
end

it 'registers an offense and corrects when the else branch contains `rescue`' do
expect_offense(<<~RUBY)
if a
^^^^ Use double pipes `||` instead.
a
else
b rescue c
end
RUBY

expect_correction(<<~RUBY)
a || (b rescue c)
RUBY
end

it 'registers an offense and corrects when the else branch contains `and`' do
expect_offense(<<~RUBY)
if a
^^^^ Use double pipes `||` instead.
a
else
b and c
end
RUBY

expect_correction(<<~RUBY)
a || (b and c)
RUBY
end
end
end

Expand Down

0 comments on commit 672bac8

Please sign in to comment.