Skip to content

Commit

Permalink
[Fix #7367] Fix an error for Style/OrAssignment
Browse files Browse the repository at this point in the history
Fixes #7367.

This PR fixes an error for `Style/OrAssignment` cop
when `then` branch body is empty.
  • Loading branch information
koic authored and bbatsov committed Sep 29, 2019
1 parent dbd7ac5 commit 3eb97cc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* [#7361](https://github.com/rubocop-hq/rubocop/issues/7361): Fix a false positive for `Style/TernaryParentheses` when only the closing parenthesis is used in the last line of condition. ([@koic][])
* [#7369](https://github.com/rubocop-hq/rubocop/issues/7369): Fix an infinite loop error for `Layout/IndentAssignment` with `Layout/IndentFirstArgument` when using multiple assignment. ([@koic][])
* [#7177](https://github.com/rubocop-hq/rubocop/issues/7177), [#7370](https://github.com/rubocop-hq/rubocop/issues/7370): When correcting alignment, do not insert spaces into string literals. ([@buehmann][])
* [#7367](https://github.com/rubocop-hq/rubocop/issues/7367): Fix an error for `Style/OrAssignment` cop when `then` branch body is empty. ([@koic][])

### Changes

Expand Down
7 changes: 6 additions & 1 deletion lib/rubocop/cop/style/or_assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ def take_variable_and_default_from_ternary(node)
end

def take_variable_and_default_from_unless(node)
variable, default = *node.if_branch
if node.if_branch
variable, default = *node.if_branch
else
variable, default = *node.else_branch
end

[variable, default]
end
end
Expand Down
18 changes: 18 additions & 0 deletions spec/rubocop/cop/style/or_assignment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,22 @@
RUBY
end
end

context 'when `then` branch body is empty' do
it 'registers an offense' do
expect_offense(<<~RUBY)
foo = nil
if foo
^^^^^^ Use the double pipe equals operator `||=` instead.
else
foo = 2
end
RUBY

expect_correction(<<~RUBY)
foo = nil
foo ||= 2
RUBY
end
end
end

0 comments on commit 3eb97cc

Please sign in to comment.