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

Add new Style/RedundantArrayConstructor cop #11886

Merged

Conversation

koic
Copy link
Member

@koic koic commented May 17, 2023

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.

# 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' }

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@koic koic force-pushed the add_new_style_redundant_array_constructor_cop branch from a7d2e8b to 5791e7b Compare May 17, 2023 12:59
@fatkodima
Copy link
Contributor

fatkodima commented May 18, 2023

I think a similar cop can be created for a string too?

# bad
String.new("string")

@bbatsov
Copy link
Collaborator

bbatsov commented May 18, 2023

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.

@koic
Copy link
Member Author

koic commented May 20, 2023

Btw, for arrays the constructor is redundant also when you're just enumerating some elements, as you can use a literal directly then.

My understanding hasn't quite caught up 💦 Can you provide an example code?

@koic
Copy link
Member Author

koic commented May 20, 2023

I think a similar cop can be created for a string too?

It seems like that case would be handled by Performance/UnfreezeString.
https://docs.rubocop.org/rubocop-performance/cops_performance.html#performanceunfreezestring

@bbatsov
Copy link
Collaborator

bbatsov commented May 30, 2023

@koic I was thinking of:

arr = Array[1, 2, 3]

Not exactly a classic constructor, but I guess you get the idea.

@koic koic force-pushed the add_new_style_redundant_array_constructor_cop branch from 5791e7b to bef3341 Compare May 30, 2023 05:43
@koic
Copy link
Member Author

koic commented May 30, 2023

Ah I didn't realize that. I think Array([1, 2, 3]) can be handled similarly. I updated this PR to handle them.

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' }
```
@koic koic force-pushed the add_new_style_redundant_array_constructor_cop branch from bef3341 to 6765030 Compare May 30, 2023 07:22
@bbatsov bbatsov merged commit a01fd9c into rubocop:master Jun 2, 2023
29 checks passed
@bbatsov
Copy link
Collaborator

bbatsov commented Jun 2, 2023

Looks great now. Thanks!

@koic koic deleted the add_new_style_redundant_array_constructor_cop branch June 2, 2023 08:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants