From b0b47e707dccf14022515b1cbf0c98974a822d80 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 19 May 2022 01:47:03 +0900 Subject: [PATCH] [Fix #289] Fix a false positive for `Performance/StringIdentifierArgument` Fixes #289. This PR fixes a false positive for `Performance/StringIdentifierArgument` when using namespaced class string argument. --- ...ive_for_performance_string_identifier_argument.md | 1 + .../cop/performance/string_identifier_argument.rb | 2 +- .../performance/string_identifier_argument_spec.rb | 12 ++++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_a_false_positive_for_performance_string_identifier_argument.md diff --git a/changelog/fix_a_false_positive_for_performance_string_identifier_argument.md b/changelog/fix_a_false_positive_for_performance_string_identifier_argument.md new file mode 100644 index 0000000000..a1222b97a9 --- /dev/null +++ b/changelog/fix_a_false_positive_for_performance_string_identifier_argument.md @@ -0,0 +1 @@ +* [#289](https://github.com/rubocop/rubocop-performance/issues/289): Fix a false positive for `Performance/StringIdentifierArgument` when using namespaced class string argument. ([@koic][]) diff --git a/lib/rubocop/cop/performance/string_identifier_argument.rb b/lib/rubocop/cop/performance/string_identifier_argument.rb index e2554d2194..63a30ddb2f 100644 --- a/lib/rubocop/cop/performance/string_identifier_argument.rb +++ b/lib/rubocop/cop/performance/string_identifier_argument.rb @@ -46,7 +46,7 @@ class StringIdentifierArgument < Base def on_send(node) return unless (first_argument = node.first_argument) return unless first_argument.str_type? - return if first_argument.value.include?(' ') + return if first_argument.value.include?(' ') || first_argument.value.include?('::') replacement = first_argument.value.to_sym.inspect diff --git a/spec/rubocop/cop/performance/string_identifier_argument_spec.rb b/spec/rubocop/cop/performance/string_identifier_argument_spec.rb index a4c478bde1..6fa6bf6e03 100644 --- a/spec/rubocop/cop/performance/string_identifier_argument_spec.rb +++ b/spec/rubocop/cop/performance/string_identifier_argument_spec.rb @@ -38,6 +38,18 @@ RUBY end + it 'does not register an offense when using cbase class string argument' do + expect_no_offenses(<<~RUBY) + Object.const_defined?('::Foo') + RUBY + end + + it 'does not register an offense when using namespaced class string argument' do + expect_no_offenses(<<~RUBY) + Object.const_defined?('Foo::Bar') + RUBY + end + it 'does not register an offense when using symbol argument for no identifier argument' do expect_no_offenses(<<~RUBY) foo('do_something')