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')