Skip to content

Commit

Permalink
[Fix rubocop#8422] Fix an error for Lint/SelfAssignment
Browse files Browse the repository at this point in the history
Fixes rubocop#8422.

Fix an error for `Lint/SelfAssignment` when using or-assignment for constant.
  • Loading branch information
koic committed Jul 31, 2020
1 parent 02daa3d commit bacc9b4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -29,6 +29,7 @@
* [#8391](https://github.com/rubocop-hq/rubocop/issues/8391): Mark `Style/ArrayCoercion` as not safe. ([@marcandre][])
* [#8406](https://github.com/rubocop-hq/rubocop/issues/8406): Improve `Style/AccessorGrouping`'s auto-correction to remove redundant blank lines. ([@koic][])
* [#8330](https://github.com/rubocop-hq/rubocop/issues/8330): Fix a false positive for `Style/MissingRespondToMissing` when defined method with inline access modifier. ([@koic][])
* [#8422](https://github.com/rubocop-hq/rubocop/issues/8422): Fix an error for `Lint/SelfAssignment` when using or-assignment for constant. ([@koic][])

### Changes

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/lint/self_assignment.rb
Expand Up @@ -40,7 +40,7 @@ def on_lvasgn(node)

def on_casgn(node)
lhs_scope, lhs_name, rhs = *node
return unless rhs.const_type?
return unless rhs&.const_type?

rhs_scope, rhs_name = *rhs
add_offense(node) if lhs_scope == rhs_scope && lhs_name == rhs_name
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/lint/self_assignment_spec.rb
Expand Up @@ -68,6 +68,12 @@
RUBY
end

it 'does not register an offense when using constant var or-assignment for constant from another scope' do
expect_no_offenses(<<~RUBY)
Foo ||= ::Foo
RUBY
end

it 'registers an offense when using multiple var self-assignment' do
expect_offense(<<~RUBY)
foo, bar = foo, bar
Expand Down

0 comments on commit bacc9b4

Please sign in to comment.