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

Remove unnecessary arguments for builtin methods. #7737

Closed
Lykos opened this issue Feb 20, 2020 · 8 comments · Fixed by #9077
Closed

Remove unnecessary arguments for builtin methods. #7737

Lykos opened this issue Feb 20, 2020 · 8 comments · Fixed by #9077
Labels
feature request stale Issues that haven't been active in a while

Comments

@Lykos
Copy link

Lykos commented Feb 20, 2020

Is your feature request related to a problem? Please describe.

I found something like array.join('') in my code recently. This can of course be replaced by array.join. There is also string.split("\n") or string.gsub(a, ''). I will try to collect more cases like this in this issue (help is welcome).

Describe the solution you'd like

For a list of important builtin functions (e.g. String#split and Array#join), if an argument is passed explicitly that leads to the same behavior as leaving the argument away, suggest to remove that argument.

Additional context

Note that I might do this myself eventually. Depends on how much time I have and how eager someone else is to take it.

@tejasbubane
Copy link
Contributor

tejasbubane commented Feb 20, 2020

With static analysis, rubocop cannot determine if the receiver is array or string. So this will only work for literals like [1, 2, 3].join('') which is rather rare IMO. Does it still make sense to add such a cop?

PS: I am willing to work on this if maintainers agree on the functionality.

@Lykos
Copy link
Author

Lykos commented Feb 20, 2020

I think we should just assume that it's an array (or similar) type in case of join. It's true that you will only get 100% correctness if you know the types. (And furthermore, you need to know the state of those types, someone might have monkey patched String#join for example) However, we have similar issues for most of the cops:

  • All the suggestions for numeric types, e.g. replacing a == 0 with a.zero?, assume that the type is some sort of numeric type.
  • All the suggestions for enumerable types, e.g. replacing a.select { |i| !p(i) } with a.reject { |i| p(i) }, assume that the type has the same meaning for select and reject as arrays.

This cop would be in line with that. In most cases, for a.join, a would either be some sort of Enumerable or at least have a definition of join that is compatible. So it would work fine for those cases. It would get some corner cases incorrect where you define a custom join method somewhere with a different semantic. But that can happen to most cops and that's what # rubocop:disable is for. If someones codebase has a lot of these, they should disable the cop. That's in line with existing cops.

Only restricting this cop on literals or cases where we are 100% sure about the receiver would make this cop almost useless.

So I would suggest just making the assumption that join, split (and whatever others come into our mind) are called on builtin types (or at least defined in line with the builtin ones).

@Drenmi
Copy link
Collaborator

Drenmi commented Feb 21, 2020

I think the request is fair. We should just make sure to mark it as unsafe. 👍

@koic
Copy link
Member

koic commented Feb 21, 2020

The default argument for Array#join is $, instead of blank string.
https://ruby-doc.org/core-2.7.0/Array.html#method-i-join

This cop seems undesirable, as it is not redundant that blank string is specified as an explicit argument.

@pocke
Copy link
Collaborator

pocke commented Feb 21, 2020

This cop seems undesirable, as it is not redundant that blank string is specified as an explicit argument.

I don't think so.
Anyone no longer uses$, and it has been deprecated in Ruby 2.7.
https://bugs.ruby-lang.org/issues/14240

So actually we can ignore $,, it means we can be aware of Array#join as def join(delm = '').

@koic
Copy link
Member

koic commented Feb 21, 2020

Ah, I didn't know this deprecation warning.

% rbenv local 2.6.5 && ruby -ve '$, = ","'
ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin17]
% rbenv local 2.7.0 && ruby -ve '$, = ","'
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-darwin17]
-e:1: warning: `$,' is deprecated

Surely, it seems that blank string is fine for default argument.

@stale
Copy link

stale bot commented Aug 19, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding!

@stale stale bot added the stale Issues that haven't been active in a while label Aug 19, 2020
tejasbubane added a commit to tejasbubane/rubocop that referenced this issue Nov 16, 2020
@stale
Copy link

stale bot commented Nov 21, 2020

This issues been automatically closed due to lack of activity. Feel free to re-open it if you ever come back to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request stale Issues that haven't been active in a while
Projects
None yet
5 participants