From ab30d85e303dab580a97a46326987d54c0b204a6 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 28 Dec 2021 04:08:58 +0900 Subject: [PATCH] [Fix #278] Fix a false positive for `Performance/StringIdentifierArgument` Fixes #278. This PR fixes a false positive for `Performance/StringIdentifierArgument` when using `attr`. `attr` may not be used because `Style/Attr` registers an offense. --- ...ositive_for_performance_string_identifier_argument.md | 1 + .../cop/performance/string_identifier_argument.rb | 5 ++++- .../cop/performance/string_identifier_argument_spec.rb | 9 +++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_false_positive_for_performance_string_identifier_argument.md diff --git a/changelog/fix_false_positive_for_performance_string_identifier_argument.md b/changelog/fix_false_positive_for_performance_string_identifier_argument.md new file mode 100644 index 0000000000..6d3eae59d4 --- /dev/null +++ b/changelog/fix_false_positive_for_performance_string_identifier_argument.md @@ -0,0 +1 @@ +* [#278](https://github.com/rubocop/rubocop-performance/issues/278): Fix a false positive for `Performance/StringIdentifierArgument` when using `attr`. ([@koic][]) diff --git a/lib/rubocop/cop/performance/string_identifier_argument.rb b/lib/rubocop/cop/performance/string_identifier_argument.rb index 52b6606a6f..a760633091 100644 --- a/lib/rubocop/cop/performance/string_identifier_argument.rb +++ b/lib/rubocop/cop/performance/string_identifier_argument.rb @@ -27,8 +27,11 @@ class StringIdentifierArgument < Base MSG = 'Use `%s` instead of `%s`.' + # NOTE: `attr` method is not included in this list as it can cause false positives in Nokogiri API. + # And `attr` may not be used because `Style/Attr` registers an offense. + # https://github.com/rubocop/rubocop-performance/issues/278 RESTRICT_ON_SEND = %i[ - alias_method attr attr_accessor attr_reader attr_writer autoload autoload? + alias_method attr_accessor attr_reader attr_writer autoload autoload? class_variable_defined? const_defined? const_get const_set const_source_location define_method instance_method method_defined? private_class_method? private_method_defined? protected_method_defined? public_class_method public_instance_method public_method_defined? diff --git a/spec/rubocop/cop/performance/string_identifier_argument_spec.rb b/spec/rubocop/cop/performance/string_identifier_argument_spec.rb index 9691afa2f7..a4c478bde1 100644 --- a/spec/rubocop/cop/performance/string_identifier_argument_spec.rb +++ b/spec/rubocop/cop/performance/string_identifier_argument_spec.rb @@ -50,4 +50,13 @@ send(':foo is :bar', foo, bar) RUBY end + + # NOTE: `attr` method is not included in this list as it can cause false positives in Nokogiri API. + # And `attr` may not be used because `Style/Attr` registers an offense. + # https://github.com/rubocop/rubocop-performance/issues/278 + it 'does not register an offense when using string argument for `attr` method' do + expect_no_offenses(<<~RUBY) + attr('foo') + RUBY + end end