Class: RuboCop::Cop::Style::ArrayIntersect

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector, TargetRubyVersion
Defined in:
lib/rubocop/cop/style/array_intersect.rb

Overview

In Ruby 3.1, Array#intersect? has been added.

This cop identifies places where (array1 & array2).any? can be replaced by array1.intersect?(array2).

The array1.intersect?(array2) method is faster than (array1 & array2).any? and is more readable.

In cases like the following, compatibility is not ensured, so it will not be detected when using block argument.

([1] & [1,2]).any? { |x| false }    # => false
[1].intersect?([1,2]) { |x| false } # => true

Examples:

# bad
(array1 & array2).any?
(array1 & array2).empty?

# good
array1.intersect?(array2)
!array1.intersect?(array2)

AllCops:ActiveSupportExtensionsEnabled: false (default)

# good
(array1 & array2).present?
(array1 & array2).blank?

AllCops:ActiveSupportExtensionsEnabled: true

# bad
(array1 & array2).present?
(array1 & array2).blank?

# good
array1.intersect?(array2)
!array1.intersect?(array2)

Cop Safety Information:

  • This cop cannot guarantee that array1 and array2 are actually arrays while method intersect? is for arrays only.

Constant Summary collapse

MSG =
'Use `%<negated>s%<receiver>s.intersect?(%<argument>s)` ' \
'instead of `(%<receiver>s & %<argument>s).%<method_name>s`.'
STRAIGHT_METHODS =
%i[present? any?].freeze
NEGATED_METHODS =
%i[blank? empty?].freeze
RESTRICT_ON_SEND =
(STRAIGHT_METHODS + NEGATED_METHODS).freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods included from TargetRubyVersion

minimum_target_ruby_version, required_minimum_ruby_version, support_target_ruby_version?

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #begin_investigation, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#active_support_bad_intersection_check?(node) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/rubocop/cop/style/array_intersect.rb', line 65

def_node_matcher :active_support_bad_intersection_check?, <<~PATTERN
  (send
    (begin
      (send $(...) :& $(...))
    ) ${:present? :any? :blank? :empty?}
  )
PATTERN

#on_send(node) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rubocop/cop/style/array_intersect.rb', line 79

def on_send(node)
  return if (parent = node.parent) && (parent.block_type? || parent.numblock_type?)
  return unless (receiver, argument, method_name = bad_intersection_check?(node))

  message = message(receiver.source, argument.source, method_name)

  add_offense(node, message: message) do |corrector|
    bang = straight?(method_name) ? '' : '!'

    corrector.replace(node, "#{bang}#{receiver.source}.intersect?(#{argument.source})")
  end
end

#regular_bad_intersection_check?(node) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/rubocop/cop/style/array_intersect.rb', line 56

def_node_matcher :regular_bad_intersection_check?, <<~PATTERN
  (send
    (begin
      (send $(...) :& $(...))
    ) ${:any? :empty?}
  )
PATTERN