Skip to content

Commit

Permalink
Merge pull request rubocop#7738 from rrosenblum/7709_redundant_condit…
Browse files Browse the repository at this point in the history
…ion_range

7709 redundant condition range
  • Loading branch information
koic committed Feb 22, 2020
2 parents 5986381 + 0e95f17 commit bad4b8a
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 142 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
### Bug fixes

* [#7719](https://github.com/rubocop-hq/rubocop/issues/7719): Fix `Style/NestedParenthesizedCalls` cop for newline. ([@tejasbubane][])
* [#7709](https://github.com/rubocop-hq/rubocop/issues/7709): Fix correction of `Style/RedundantCondition` when the else branch contains a range. ([@rrosenblum][])

## 0.80.0 (2020-02-18)

Expand Down
21 changes: 17 additions & 4 deletions lib/rubocop/cop/style/redundant_condition.rb
Expand Up @@ -46,7 +46,7 @@ def on_if(node)
def autocorrect(node)
lambda do |corrector|
if node.ternary?
corrector.replace(range_of_offense(node), '||')
correct_ternary(corrector, node)
elsif node.modifier_form? || !node.else_branch
corrector.replace(node.source_range, node.if_branch.source)
else
Expand Down Expand Up @@ -90,9 +90,13 @@ def use_if_branch?(else_branch)
end

def else_source(else_branch)
wrap_else =
else_branch.basic_conditional? && else_branch.modifier_form?
wrap_else ? "(#{else_branch.source})" : else_branch.source
if else_branch.basic_conditional? &&
else_branch.modifier_form? ||
else_branch.range_type?
"(#{else_branch.source})"
else
else_branch.source
end
end

def make_ternary_form(node)
Expand All @@ -106,6 +110,15 @@ def make_ternary_form(node)
ternary_form
end
end

def correct_ternary(corrector, node)
corrector.replace(range_of_offense(node), '||')

return unless node.else_branch.range_type?

corrector.insert_before(node.else_branch.loc.expression, '(')
corrector.insert_after(node.else_branch.loc.expression, ')')
end
end
end
end
Expand Down

0 comments on commit bad4b8a

Please sign in to comment.