Skip to content

Commit

Permalink
Merge pull request #12439 from koic/fix_a_false_positive_for_lint_sel…
Browse files Browse the repository at this point in the history
…f_assignment

[Fix #12435] Fix a false positive for `Lint/SelfAssignment`
  • Loading branch information
koic committed Dec 5, 2023
2 parents 3b31891 + c7ff1a4 commit 3745a3c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/fix_a_false_positive_for_lint_self_assignment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12435](https://github.com/rubocop/rubocop/issues/12435): Fix a false positive for `Lint/SelfAssignment` when using attribute assignment with method call with arguments. ([@koic][])
1 change: 1 addition & 0 deletions lib/rubocop/cop/lint/self_assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def handle_key_assignment(node)

def handle_attribute_assignment(node)
first_argument = node.first_argument
return unless first_argument.respond_to?(:arguments) && first_argument.arguments.empty?

if first_argument.call_type? &&
node.receiver == first_argument.receiver &&
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/lint/self_assignment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@
RUBY
end

it 'does not register an offense when using attribute assignment with method call with arguments' do
expect_no_offenses(<<~RUBY)
foo.bar = foo.bar(arg)
RUBY
end

it 'does not register an offense when using attribute assignment with literals' do
expect_no_offenses(<<~RUBY)
foo.bar = true
RUBY
end

it 'registers an offense when using []= self-assignment with same string literals' do
expect_offense(<<~RUBY)
foo["bar"] = foo["bar"]
Expand Down

0 comments on commit 3745a3c

Please sign in to comment.