Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #346] Fix a false positive for Performance/StringIdentifierArgument #349

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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