Skip to content

Commit

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

This PR fixes an error for `Style/SwapValues`
when assigning receiver object at `def`.
  • Loading branch information
koic authored and bbatsov committed Feb 3, 2022
1 parent 0bbc02e commit 7e09781
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_style_swap_values.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#10394](https://github.com/rubocop/rubocop/issues/10394): Fix an error for `Style/SwapValues` when assigning receiver object at `def`. ([@koic][])
2 changes: 2 additions & 0 deletions lib/rubocop/cop/style/swap_values.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def swapping_values?(tmp_assign, x_assign, y_assign)
end

def simple_assignment?(node)
return false unless node.respond_to?(:type)

SIMPLE_ASSIGNMENT_TYPES.include?(node.type)
end

Expand Down
7 changes: 7 additions & 0 deletions spec/rubocop/cop/style/swap_values_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,11 @@
y = not_a_tmp
RUBY
end

it 'does not register an offense when assigning receiver object at `def`' do
expect_no_offenses(<<~RUBY)
def (foo = Object.new).do_something
end
RUBY
end
end

0 comments on commit 7e09781

Please sign in to comment.