-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Add new Style/RedundantArrayConstructor
cop
#11886
Add new Style/RedundantArrayConstructor
cop
#11886
Conversation
a7d2e8b
to
5791e7b
Compare
I think a similar cop can be created for a string too? # bad
String.new("string") |
Btw, for arrays the constructor is redundant also when you're just enumerating some elements, as you can use a literal directly then. Not sure if it's worth covering this here or separately, though. |
My understanding hasn't quite caught up 💦 Can you provide an example code? |
It seems like that case would be handled by |
@koic I was thinking of: arr = Array[1, 2, 3] Not exactly a classic constructor, but I guess you get the idea. |
5791e7b
to
bef3341
Compare
Ah I didn't realize that. I think |
Follow up rubocop#11874 (comment). Checks for the instantiation of array using redundant `Array` constructor. Autocorrect replaces to array literal which is the simplest and fastest. ```ruby # bad Array.new([]) Array[] Array([]) Array.new(['foo', 'foo', 'foo']) Array['foo', 'foo', 'foo'] Array(['foo', 'foo', 'foo']) # good [] ['foo', 'foo', 'foo'] Array.new(3, 'foo') Array.new(3) { 'foo' } ```
bef3341
to
6765030
Compare
Looks great now. Thanks! |
Follow up #11874 (comment).
Checks for the instantiation of array using redundant
Array
constructor. Autocorrect replaces to array literal which is the simplest and fastest.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.