Skip to content

Commit

Permalink
[Fix rubocop#278] Fix a false positive for `Performance/StringIdentif…
Browse files Browse the repository at this point in the history
…ierArgument`

Fixes rubocop#278.

This PR fixes a false positive for `Performance/StringIdentifierArgument` when using `attr`.
`attr` may not be used because `Style/Attr` registers an offense.
  • Loading branch information
koic committed Dec 27, 2021
1 parent e08862c commit 157a3ca
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#278](https://github.com/rubocop/rubocop-performance/issues/278): Fix a false positive for `Performance/StringIdentifierArgument` when using `attr`. ([@koic][])
5 changes: 4 additions & 1 deletion lib/rubocop/cop/performance/string_identifier_argument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ class StringIdentifierArgument < Base

MSG = 'Use `%<symbol_arg>s` instead of `%<string_arg>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?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 157a3ca

Please sign in to comment.