KEMBAR78
Register array intersection size checks as offenses under `Style/ArrayIntersect` by lovro-bikic · Pull Request #14448 · rubocop/rubocop · GitHub
Skip to content

Conversation

lovro-bikic
Copy link
Contributor

@lovro-bikic lovro-bikic commented Aug 14, 2025

Registers the following code as offenses under Style/ArrayIntersect:

(a & b).size > 0
(a & b).size != 0
(a & b).size.positive?

(a & b).size == 0
(a & b).size.zero?

and autocorrects to:

a.intersect?(b)

!a.intersect?(b)

In addition to #size, the patch takes into account #length and #count methods as well.

real-world-rails report:

Report
# https://github.com/ivaldi/brimir/blob/35ead7a12471c8f1d1881c7c5ebdb00587198691/app/models/ability.rb#L55
(reply.ticket.label_ids & user.label_ids).size > 0

# https://github.com/ivaldi/brimir/blob/35ead7a12471c8f1d1881c7c5ebdb00587198691/app/models/ability.rb#L64
ticket.user == user || (ticket.label_ids & user.label_ids).size > 0

# https://github.com/ivaldi/brimir/blob/35ead7a12471c8f1d1881c7c5ebdb00587198691/app/models/ability.rb#L83
ticket.user == user || (ticket.label_ids & user.label_ids).size > 0 ||

# https://github.com/ivaldi/brimir/blob/35ead7a12471c8f1d1881c7c5ebdb00587198691/app/models/ability.rb#L89
(reply.ticket.label_ids & user.label_ids).size > 0

# https://github.com/ivaldi/brimir/blob/35ead7a12471c8f1d1881c7c5ebdb00587198691/test/models/ticket_test.rb#L33
assert dave == ticket.user || (ticket.label_ids & dave.label_ids).size > 0

# https://github.com/instructure/canvas-lms/blob/b5fdc8cec7484881e3994f128a541fd232f3f85a/app/models/appointment_group.rb#L149
if (@new_sub_context_codes & context_subs).count == 0

# https://github.com/discourse/discourse/blob/758b9dd0ba6f7beb6673db65c3b471b0bbacf0a1/app/models/post_analyzer.rb#L66
(Post.allowed_image_classes & dom_class.split).count > 0

# https://github.com/discourse/discourse/blob/758b9dd0ba6f7beb6673db65c3b471b0bbacf0a1/lib/discourse_tagging.rb#L136
(tag_ids & parent_tag_ids).size == 0 ? parent_tag_ids.first : nil

# https://github.com/discourse/discourse/blob/758b9dd0ba6f7beb6673db65c3b471b0bbacf0a1/lib/discourse_tagging.rb#L157
next if (tag_ids & parent_tag_ids).size > 0 # tag already has a parent tag

# https://github.com/discourse/discourse/blob/758b9dd0ba6f7beb6673db65c3b471b0bbacf0a1/lib/site_settings/validations.rb#L42
if (category_ids & default_categories_selected).size > 0

# https://github.com/discourse/discourse/blob/758b9dd0ba6f7beb6673db65c3b471b0bbacf0a1/lib/site_settings/validations.rb#L113
validate_error :default_tags_already_selected if (tag_names & default_tags_selected).size > 0

# https://github.com/discourse/discourse/blob/758b9dd0ba6f7beb6673db65c3b471b0bbacf0a1/lib/upload_recovery.rb#L23
next if (Post.allowed_image_classes & dom_class.split).count > 0

# https://github.com/ministryofjustice/ds-auth/blob/90662947666f71d1aba5590f424057d4632cda8a/app/policies/user_policy.rb#L41
(user.organisation_ids & record.organisation_ids).length > 0

# https://github.com/samvera-labs/hyku/blob/00a8e6d373d8881557cc9b62d651473a174e525b/app/models/sushi/platform_report.rb#L132
return [] if metric_type_in_params && (metric_types & (ALLOWED_METRIC_TYPES - ['Searches_Platform'])).count.zero?

# https://github.com/loomio/loomio/blob/479656b2c26fb83afde18b65eed6b1d167b9a067/app/controllers/api/v1/memberships_controller.rb#L71
(user.group_ids & current_user.adminable_group_ids).length > 0

# https://github.com/sunlightlabs/politwoops/blob/e1ce513ec75aa2ffc16fd4816630bdc3a1dc6f89/app/controllers/admin/admin_controller.rb#L19
from_monitoring_host = (ips.intersection(monitoring_ips).size > 0)

# https://github.com/zammad/zammad/blob/15c23974f5e21a5a7db0d1563c21d541b152b344/app/models/core_workflow/condition/contains.rb#L5
(value & condition_value).count.positive?

# https://github.com/zammad/zammad/blob/15c23974f5e21a5a7db0d1563c21d541b152b344/app/models/core_workflow/condition/contains_all_not.rb#L5
(value & condition_value).count.zero?

# https://github.com/zammad/zammad/blob/15c23974f5e21a5a7db0d1563c21d541b152b344/app/models/core_workflow/custom/admin_show_group_list_for_agents.rb#L33
return 'show' if (Array.wrap(params['role_ids']).map(&:to_i) & Role.with_permissions('ticket.agent').pluck(:id)).count.positive?

# https://github.com/zammad/zammad/blob/15c23974f5e21a5a7db0d1563c21d541b152b344/app/policies/overview_policy.rb#L42
(user.role_ids.to_set & record.role_ids.to_set).count.zero?

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.

# * `(array1 & array2).any?`
# * `(array1.intersection(array2)).any?`
# * `array1.any? { |elem| array2.member?(elem) }`
# * `(array1 & array2).count > 0`
Copy link
Contributor Author

@lovro-bikic lovro-bikic Aug 14, 2025

Choose a reason for hiding this comment

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

I didn't add size and length here, it didn't seem that important. Should I include them as well?

Copy link
Collaborator

Choose a reason for hiding this comment

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

At least one of them, as size and length are aliases, but count is a different method altogether.

Copy link
Collaborator

Choose a reason for hiding this comment

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

(and without a block, normally you shouldn't use count IMO)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At least one of them, as size and length are aliases

Updated

@lovro-bikic lovro-bikic force-pushed the array-intersect-false-negatives branch from 4ef4f2f to a2489a1 Compare August 20, 2025 05:45
@bbatsov bbatsov merged commit 7cb7438 into rubocop:master Aug 20, 2025
22 checks passed
@lovro-bikic lovro-bikic deleted the array-intersect-false-negatives branch August 29, 2025 18:05
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