Class: RuboCop::Cop::Style::ArrayIntersect
- 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.
- source,ruby
([1] & [1,2]).any? { |x| false } # => false [1].intersect?() { |x| false } # => true
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? none?].freeze
- RESTRICT_ON_SEND =
(STRAIGHT_METHODS + NEGATED_METHODS).freeze
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #active_support_bad_intersection_check?(node) ⇒ Object
- #on_send(node) ⇒ Object
- #regular_bad_intersection_check?(node) ⇒ Object
Methods included from AutoCorrector
Methods included from TargetRubyVersion
maximum_target_ruby_version, minimum_target_ruby_version, required_maximum_ruby_version, required_minimum_ruby_version, support_target_ruby_version?
Methods inherited from Base
#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #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, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version
Methods included from ExcludeLimit
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
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
#active_support_bad_intersection_check?(node) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/rubocop/cop/style/array_intersect.rb', line 66 def_node_matcher :active_support_bad_intersection_check?, <<~PATTERN (send (begin (send $(...) :& $(...)) ) ${:present? :any? :blank? :empty? :none?} ) PATTERN |
#on_send(node) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/rubocop/cop/style/array_intersect.rb', line 80 def on_send(node) return if node.block_literal? return unless (receiver, argument, method_name = bad_intersection_check?(node)) = (receiver.source, argument.source, method_name) add_offense(node, message: ) do |corrector| bang = straight?(method_name) ? '' : '!' corrector.replace(node, "#{bang}#{receiver.source}.intersect?(#{argument.source})") end end |
#regular_bad_intersection_check?(node) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/rubocop/cop/style/array_intersect.rb', line 57 def_node_matcher :regular_bad_intersection_check?, <<~PATTERN (send (begin (send $(...) :& $(...)) ) ${:any? :empty? :none?} ) PATTERN |