Skip to content

Fix autocorrection syntax error for Performance/Count with multiline calls #504

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

Merged
merged 1 commit into from
Jun 14, 2025

Conversation

lovro-bikic
Copy link
Contributor

@lovro-bikic lovro-bikic commented Jun 13, 2025

Currently, Performance/Count will autocorrect:

foo
  .select(&:something)
  .count
  .positive?

to:

foo
  .count(&:something)

  .positive?

which is not valid syntax. This PR fixes autocorrection to:

foo
  .count(&:something)
  .positive?

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.

Comment on lines +4 to 20
shared_examples 'selectors' do |selector, negate_condition|
it "registers an offense for using array.#{selector}...size" do
expect_offense(<<~RUBY, selector: selector)
[1, 2, 3].#{selector} { |e| e.even? }.size
^{selector}^^^^^^^^^^^^^^^^^^^^^ Use `count` instead of `#{selector}...size`.
RUBY

if negate_condition
expect_correction(<<~RUBY)
[1, 2, 3].count { |e| !(e.even?) }
RUBY
else
expect_correction(<<~RUBY)
[1, 2, 3].count { |e| e.even? }
RUBY
end
end
Copy link
Contributor Author

@lovro-bikic lovro-bikic Jun 13, 2025

Choose a reason for hiding this comment

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

I took the liberty of adding correction expectations for all examples which were missing them; let me know if I should move this to a separate PR.

@koic koic merged commit 7758046 into rubocop:master Jun 14, 2025
15 checks passed
@koic
Copy link
Member

koic commented Jun 14, 2025

Thanks!

@lovro-bikic lovro-bikic deleted the performance-count-multiline-call branch June 14, 2025 07:50
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.

2 participants