Skip to content

[Fix #532] Fix false negatives for Performance/ChainArrayAllocation - #533

Merged
koic merged 1 commit into
rubocop:masterfrom
koic:fix_chain_array_allocation_false_negatives
Aug 11, 2026
Merged

[Fix #532] Fix false negatives for Performance/ChainArrayAllocation#533
koic merged 1 commit into
rubocop:masterfrom
koic:fix_chain_array_allocation_false_negatives

Conversation

@koic

@koic koic commented Aug 10, 2026

Copy link
Copy Markdown
Member

flat_map, filter_map, and collect_concat always return a new array, but they were missing from ALWAYS_RETURNS_NEW_ARRAY, so a chain built on them was only partially matched or not matched at all. nodes.flat_map(&:file_ids).compact.uniq reported one offense while the same-shaped map version reported two, and rewriting a flagged chain around flat_map silenced the cop without fixing the allocation it complained about.

Recognize the three methods as chain receivers. They are not added to HAS_MUTATION_ALTERNATIVE because no bang variants exist, so they are never suggested for ! conversion.

Fixes #532.


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.

…cation`

`flat_map`, `filter_map`, and `collect_concat` always return a new array,
but they were missing from `ALWAYS_RETURNS_NEW_ARRAY`, so a chain built
on them was only partially matched or not matched at all.
`nodes.flat_map(&:file_ids).compact.uniq` reported one offense while
the same-shaped `map` version reported two, and rewriting a flagged chain
around `flat_map` silenced the cop without fixing the allocation it complained about.

Recognize the three methods as chain receivers. They are not added to
`HAS_MUTATION_ALTERNATIVE` because no bang variants exist, so they are
never suggested for `!` conversion.

Fixes rubocop#532.
@koic
koic merged commit 72eb77d into rubocop:master Aug 11, 2026
15 checks passed
@koic
koic deleted the fix_chain_array_allocation_false_negatives branch August 11, 2026 17:31
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.

ChainArrayAllocation misses flat_map and filter_map, so rewriting a chain around them silences the offense without fixing it

1 participant