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 #12842] Add new Style/SendWithLiteralMethodName
cop
#12850
base: master
Are you sure you want to change the base?
Conversation
09071bc
to
52a1e53
Compare
I like the cop, but I'm not sure about the name. Perhaps this cop should consider constants as well (e.g. |
I don't think this cop should concern itself with constants (at least by default). Surely I used a constant to intentionally factor it out, unless the constant is referenced only once I'd want to keep it. |
Since a constant is generally immutable by definition, I think it can always be targeted without needing options. Are there any valid cases for excluding a constant? |
There are barely any cases I could find that use It uses a constant to have one place that holds which browser is being tested. I don't believe the code will improve by calling the method directly. |
Fixes rubocop#12842. ## Summary Detects the use of the `public_send` method with a literal method name argument. Since the `send` method can be used to call private methods, by default, only the `public_send` method is detected. ```ruby # bad obj.public_send(:method_name) obj.public_send('method_name') # good obj.method_name ``` ## Safety This cop is not safe because it can incorrectly detect based on the receiver. Additionally, when `AllowSend` is set to `true`, it cannot determine whether the `send` method being detected is calling a private method. ## `AllowSend` option This cop has `AllowSend` option. ### AllowSend: true (default) ```ruby # good obj.send(:method_name) obj.send('method_name') obj.__send__(:method_name) obj.__send__('method_name') ``` ### AllowSend: false ```ruby # bad obj.send(:method_name) obj.send('method_name') obj.__send__(:method_name) obj.__send__('method_name') # good obj.method_name ```
52a1e53
to
459aff0
Compare
Style/StaticSend
copStyle/SendWithLiteralMethodName
cop
I see! Since using constants is intentional in some cases, it seems better to respect the user's intent and continue to target only string literals and symbol literals as is.
The cop name |
Fixes #12842.
Summary
Detects the use of the
public_send
method with a literal method name argument. Since thesend
method can be used to call private methods, by default, only thepublic_send
method is detected.Safety
This cop is not safe because it can incorrectly detect based on the receiver. Additionally, when
AllowSend
is set totrue
, it cannot determine whether thesend
method being detected is calling a private method.AllowSend
optionThis cop has
AllowSend
option.AllowSend: true (default)
AllowSend: false
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.