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.

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)

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, 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, 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


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

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

#on_send(node) ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rubocop/cop/style/array_intersect.rb', line 70

def on_send(node)
  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|
    if straight?(method_name)
      corrector.replace(node, "#{receiver.source}.intersect?(#{argument.source})")
    else
      corrector.replace(node, "!#{receiver.source}.intersect?(#{argument.source})")
    end
  end
end

#regular_bad_intersection_check?(node) ⇒ Object


47
48
49
50
51
52
53
# File 'lib/rubocop/cop/style/array_intersect.rb', line 47

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