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 false negatives for Style/EmptyLiteral #13173

Merged
merged 1 commit into from
Aug 31, 2024

Conversation

vlad-pisanov
Copy link
Contributor

@vlad-pisanov vlad-pisanov commented Aug 30, 2024

Improve Style/EmptyLiteral to detect other common ways of creating empty literals.

Array.new      # ✔️ existing offense
Hash.new       # ✔️ existing offense

Array[]        # ⚠️ new offense
Array([])      # ⚠️ new offense
Array.new([])  # ⚠️ new offense
Hash[]         # ⚠️ new offense
Hash([])       # ⚠️ new offense
Hash.new([])   # ⚠️ new offense

Also: improve offense message by including the actual offense source


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.

@vlad-pisanov vlad-pisanov force-pushed the vp_empty_literal_1 branch 2 times, most recently from 66a1f55 to fe73343 Compare August 30, 2024 05:35
Comment on lines 81 to 82
current = array_with_index(node) ? 'Array[]' : 'Array.new'
format(ARR_MSG, current: current)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you update as below and adjust the specs to have more appropriate message?

Suggested change
current = array_with_index(node) ? 'Array[]' : 'Array.new'
format(ARR_MSG, current: current)
format(ARR_MSG, current: node.source)

Comment on lines 84 to 85
current = hash_with_index(node) ? 'Hash[]' : 'Hash.new'
format(HASH_MSG, current: current)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

Suggested change
current = hash_with_index(node) ? 'Hash[]' : 'Hash.new'
format(HASH_MSG, current: current)
format(HASH_MSG, current: node.source)

…ash[]`, `Array.new([])` and `Hash.new([])`
expect_correction(<<~RUBY)
test = []
RUBY
context 'initializer resulting in an empty array literal' do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

combined existing and new cases into shared examples for readability

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for being considerate. However, as I mentioned in the comment below, shared_examples do not always improve readability. Can you respect the existing spec descriptions?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in that particular case the shared examples are OK, given that we're essentially testing 6 times for the same thing.

expect_correction(<<~RUBY)
test = []
RUBY
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shared_examples don't always improve readability and reproducibility. Could you expand them into individual tests, as in the original specs?

@bbatsov bbatsov merged commit 0431023 into rubocop:master Aug 31, 2024
22 checks passed
@franzliedke
Copy link
Contributor

@vlad-pisanov @bbatsov Why is Hash.new([]) an offense? That parameter is the default value for unknown keys. (It's better to do this with the block form, but that's a different discussion.) That's not the same as a literal {}, or what am I missing?

@bbatsov
Copy link
Collaborator

bbatsov commented Sep 2, 2024

@franzliedke That's an oversight that's fixed on master.

@bbatsov
Copy link
Collaborator

bbatsov commented Sep 2, 2024

See #13178

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.

4 participants