Skip to content

Commit

Permalink
[Fix: #6668] Fix autocorrect for UnneededCondition with unless
Browse files Browse the repository at this point in the history
  • Loading branch information
mvz committed Jan 23, 2019
1 parent 4dbc9dc commit c85e485
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -12,6 +12,7 @@
* [#6648](https://github.com/rubocop-hq/rubocop/issues/6648): Fix auto-correction of `Style/EmptyLiteral` when `Hash.new` is passed as the first argument to `super`. ([@rrosenblum][])
* [#6351](https://github.com/rubocop-hq/rubocop/pull/6351): Fix a false positive for `Layout/ClosingParenthesisIndentation` when first argument is multiline. ([@antonzaytsev][])
* [#6689](https://github.com/rubocop-hq/rubocop/pull/6689): Support more complex argument patterns on `Rails/Validation` auto-correction. ([@r7kamura][])
* [#6668](https://github.com/rubocop-hq/rubocop/issues/6668): Fix autocorrection for `Style/UnneededCondition` when conditional has the `unless` form. ([@mvz][])

## 0.63.1 (2019-01-22)

Expand Down
5 changes: 3 additions & 2 deletions lib/rubocop/cop/style/unneeded_condition.rb
Expand Up @@ -96,8 +96,9 @@ def else_source(else_branch)
end

def make_ternary_form(node)
ternary_form = [node.if_branch.source,
else_source(node.else_branch)].join(' || ')
_condition, if_branch, else_branch = *node
ternary_form = [if_branch.source,
else_source(else_branch)].join(' || ')

if node.parent && node.parent.send_type?
"(#{ternary_form})"
Expand Down
53 changes: 53 additions & 0 deletions spec/rubocop/cop/style/unneeded_condition_spec.rb
Expand Up @@ -225,4 +225,57 @@
end
end
end

context 'when inverted condition (unless)' do
it 'registers no offense' do
expect_no_offenses(<<-RUBY.strip_indent)
unless a
b
else
c
end
RUBY
end

context 'when condition and else branch are same' do
it 'registers an offense' do
expect_offense(<<-RUBY.strip_indent)
unless b
^^^^^^^^ Use double pipes `||` instead.
y(x, z)
else
b
end
RUBY
end

context 'when unless branch is complex' do
it 'registers no offense' do
expect_no_offenses(<<-RUBY.strip_indent)
unless b
c
d
else
b
end
RUBY
end
end
end

describe '#autocorrection' do
it 'auto-corrects offense' do
new_source = autocorrect_source(<<-RUBY.strip_indent)
unless b
c
else
b
end
RUBY
expect(new_source).to eq(<<-RUBY.strip_indent)
b || c
RUBY
end
end
end
end

0 comments on commit c85e485

Please sign in to comment.