From 839e35b80169efcd70b7528c335931a3396468a0 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 9 Jul 2020 19:20:48 +0900 Subject: [PATCH] [Fix #147] Fix an error for `Performance/AncestorsInclude` Fixes #147. This PR fixes an error for `Performance/AncestorsInclude` when using `ancestors.include?` without receiver. --- CHANGELOG.md | 4 ++++ lib/rubocop/cop/performance/ancestors_include.rb | 4 +++- .../rubocop/cop/performance/ancestors_include_spec.rb | 11 +++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index df8e78e0a7..baff248f7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## master (unreleased) +### Bug fixes + +* [#147](https://github.com/rubocop-hq/rubocop-performance/issues/147): Fix an error for `Performance/AncestorsInclude` when using `ancestors.include?` without receiver. ([@koic][]) + ### Changes * [#149](https://github.com/rubocop-hq/rubocop-performance/pull/149): Mark `Performance/AncestorsInclude` as unsafe. ([@eugeneius][]) diff --git a/lib/rubocop/cop/performance/ancestors_include.rb b/lib/rubocop/cop/performance/ancestors_include.rb index ab02fac7c7..f50b57d4b6 100644 --- a/lib/rubocop/cop/performance/ancestors_include.rb +++ b/lib/rubocop/cop/performance/ancestors_include.rb @@ -35,7 +35,9 @@ def on_send(node) def autocorrect(node) ancestors_include_candidate?(node) do |subclass, superclass| lambda do |corrector| - corrector.replace(node, "#{subclass.source} <= #{superclass.source}") + subclass_source = subclass ? subclass.source : 'self' + + corrector.replace(node, "#{subclass_source} <= #{superclass.source}") end end end diff --git a/spec/rubocop/cop/performance/ancestors_include_spec.rb b/spec/rubocop/cop/performance/ancestors_include_spec.rb index d870eff2d7..cd81456018 100644 --- a/spec/rubocop/cop/performance/ancestors_include_spec.rb +++ b/spec/rubocop/cop/performance/ancestors_include_spec.rb @@ -14,6 +14,17 @@ RUBY end + it 'registers an offense and corrects when using `ancestors.include?` without receiver' do + expect_offense(<<~RUBY) + ancestors.include?(Klass) + ^^^^^^^^^^^^^^^^^^ Use `<=` instead of `ancestors.include?`. + RUBY + + expect_correction(<<~RUBY) + self <= Klass + RUBY + end + it 'does not register an offense when using `<=`' do expect_no_offenses(<<~RUBY) Class <= Kernel