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?` or `(array1.intersection(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
NOTE: Although ‘Array#intersection` can take zero or multiple arguments, only cases where exactly one argument is provided can be replaced with `Array#intersect?` and are handled by this cop.
Constant Summary collapse
- PREDICATES =
%i[any? empty? none?].to_set.freeze
- ACTIVE_SUPPORT_PREDICATES =
(PREDICATES + %i[present? blank?]).freeze
- MSG =
'Use `%<negated>s%<receiver>s%<dot>sintersect?(%<argument>s)` ' \ 'instead of `%<existing>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
- #bad_intersection_check?(node, predicates) ⇒ Object
- #on_send(node) ⇒ Object (also: #on_csend)
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_gem_version, #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
permalink #bad_intersection_check?(node, predicates) ⇒ Object
[View source]
70 71 72 73 74 75 76 77 78 |
# File 'lib/rubocop/cop/style/array_intersect.rb', line 70 def_node_matcher :bad_intersection_check?, <<~PATTERN (call { (begin (send $_ :& $_)) (call $_ :intersection $_) } $%1 ) PATTERN |
permalink #on_send(node) ⇒ Object Also known as: on_csend
[View source]
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/rubocop/cop/style/array_intersect.rb', line 86 def on_send(node) return if node.block_literal? return unless (receiver, argument, method_name = bad_intersection?(node)) dot = node.loc.dot.source = (receiver.source, argument.source, method_name, dot, node.source) add_offense(node, message: ) do |corrector| bang = straight?(method_name) ? '' : '!' corrector.replace(node, "#{bang}#{receiver.source}#{dot}intersect?(#{argument.source})") end end |