From c343832b1a85fa327f0ba7863b5adba0dc476ccb Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 31 Jul 2020 16:12:44 +0900 Subject: [PATCH] [Fix #8422] Fix an error for `Lint/SelfAssignment` Fixes #8422. Fix an error for `Lint/SelfAssignment` when using or-assignment for constant. --- CHANGELOG.md | 1 + lib/rubocop/cop/lint/self_assignment.rb | 2 +- spec/rubocop/cop/lint/self_assignment_spec.rb | 6 ++++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bde58d6d23f..7c40f31afbe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rubocop/cop/lint/self_assignment.rb b/lib/rubocop/cop/lint/self_assignment.rb index 1eac628e923..9d453f09a0e 100644 --- a/lib/rubocop/cop/lint/self_assignment.rb +++ b/lib/rubocop/cop/lint/self_assignment.rb @@ -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 diff --git a/spec/rubocop/cop/lint/self_assignment_spec.rb b/spec/rubocop/cop/lint/self_assignment_spec.rb index 203343b63b4..9a1446d2e21 100644 --- a/spec/rubocop/cop/lint/self_assignment_spec.rb +++ b/spec/rubocop/cop/lint/self_assignment_spec.rb @@ -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