Skip to content

Commit

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

Fixes rubocop#346.

This PR fixes a false positive for `Performance/StringIdentifierArgument`
when using a command method with receiver. It makes that cop to allow
some command methods that don't normally specify a receiver.
  • Loading branch information
koic committed Mar 15, 2023
1 parent 59a4509 commit 80943f8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#346](https://github.com/rubocop/rubocop-performance/issues/346): Fix a false positive for `Performance/StringIdentifierArgument` when using a command method with receiver. ([@koic][])
25 changes: 15 additions & 10 deletions lib/rubocop/cop/performance/string_identifier_argument.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,33 @@ class StringIdentifierArgument < Base

MSG = 'Use `%<symbol_arg>s` instead of `%<string_arg>s`.'

COMMAND_METHODS = %i[
alias_method attr_accessor attr_reader attr_writer autoload autoload? private private_constant
protected public public_constant module_function
].freeze

# 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_accessor attr_reader attr_writer autoload autoload?
RESTRICT_ON_SEND = (%i[
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?
remove_class_variable remove_method undef_method class_variable_get class_variable_set
deprecate_constant module_function private private_constant protected public public_constant
remove_const ruby2_keywords
define_singleton_method instance_variable_defined? instance_variable_get instance_variable_set
method public_method public_send remove_instance_variable respond_to? send singleton_method
__send__
].freeze
deprecate_constant remove_const ruby2_keywords define_singleton_method instance_variable_defined?
instance_variable_get instance_variable_set method public_method public_send remove_instance_variable
respond_to? send singleton_method __send__
] + COMMAND_METHODS).freeze

def on_send(node)
return if COMMAND_METHODS.include?(node.method_name) && node.receiver
return unless (first_argument = node.first_argument)
return unless first_argument.str_type?
return if first_argument.value.include?(' ') || first_argument.value.include?('::')

replacement = first_argument.value.to_sym.inspect
first_argument_value = first_argument.value
return if first_argument_value.include?(' ') || first_argument_value.include?('::')

replacement = first_argument_value.to_sym.inspect

message = format(MSG, symbol_arg: replacement, string_arg: first_argument.source)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
end
end

RuboCop::Cop::Performance::StringIdentifierArgument::COMMAND_METHODS.each do |method|
it "does not register an offense when using string argument for `#{method}` method with receiver" do
expect_no_offenses(<<~RUBY)
obj.#{method}('do_something')
RUBY
end
end

it 'does not register an offense when no arguments' do
expect_no_offenses(<<~RUBY)
send
Expand Down

0 comments on commit 80943f8

Please sign in to comment.