Class: RuboCop::Cop::Style::ArrayIntersect
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
::RuboCop::Cop::TargetRubyVersion,
::RuboCop::Cop::AutoCorrector,
::RuboCop::Cop::Base,
::RuboCop::ExcludeLimit,
NodePattern::Macros,
RuboCop::AST::Sexp
|
|
|
Instance Chain:
self,
::RuboCop::Cop::Base,
::RuboCop::Cop::AutocorrectLogic,
::RuboCop::Cop::IgnoredNode,
::RuboCop::Util,
RuboCop::AST::Sexp
|
|
| Inherits: |
RuboCop::Cop::Base
|
| 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? -
(array1.intersection(array2)).any? -
array1.any? { |elem| array2.member?(elem) } -
(array1 & array2).count > 0 -
(array1 & array2).size > 0
can be replaced with array1.intersect?(array2).
array1.intersect?(array2) is faster and 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
|
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
-
ACTIVE_SUPPORT_PREDICATES =
# File 'lib/rubocop/cop/style/array_intersect.rb', line 89(PREDICATES + %i[present? blank?]).freeze
-
ARRAY_SIZE_METHODS =
# File 'lib/rubocop/cop/style/array_intersect.rb', line 91%i[count length size].to_set.freeze
-
MSG =
# File 'lib/rubocop/cop/style/array_intersect.rb', line 137'Use `%<replacement>s` instead of `%<existing>s`.' -
NEGATED_METHODS =
# File 'lib/rubocop/cop/style/array_intersect.rb', line 139%i[blank? empty? none? == zero?].freeze
-
PREDICATES =
# File 'lib/rubocop/cop/style/array_intersect.rb', line 88%i[any? empty? none?].to_set.freeze
-
RESTRICT_ON_SEND =
# File 'lib/rubocop/cop/style/array_intersect.rb', line 140(STRAIGHT_METHODS + NEGATED_METHODS).freeze
-
STRAIGHT_METHODS =
# File 'lib/rubocop/cop/style/array_intersect.rb', line 138%i[present? any? > positive? !=].freeze
::RuboCop::Cop::Base - Inherited
Class Attribute Summary
::RuboCop::Cop::AutoCorrector - Extended
::RuboCop::Cop::Base - Inherited
| .gem_requirements, .lint?, | |
| .support_autocorrect? | Returns if class supports autocorrect. |
| .support_multiple_source? | Override if your cop should be called repeatedly for multiple investigations Between calls to |
Class Method Summary
::RuboCop::Cop::TargetRubyVersion - Extended
| maximum_target_ruby_version, minimum_target_ruby_version, required_maximum_ruby_version, required_minimum_ruby_version, support_target_ruby_version? |
::RuboCop::Cop::Base - Inherited
| .autocorrect_incompatible_with | List of cops that should not try to autocorrect at the same time as this cop. |
| .badge | Naming. |
| .callbacks_needed, .cop_name, .department, | |
| .documentation_url | Returns a url to view this cops documentation online. |
| .exclude_from_registry | Call for abstract Cop classes. |
| .inherited, | |
| .joining_forces | Override and return the Force class(es) you need to join. |
| .match? | Returns true if the cop name or the cop namespace matches any of the given names. |
| .new, | |
| .requires_gem | Register a version requirement for the given gem name. |
| .restrict_on_send | |
::RuboCop::ExcludeLimit - Extended
| exclude_limit | Sets up a configuration option to have an exclude limit tracked. |
| transform | |
Instance Attribute Summary
::RuboCop::Cop::Base - Inherited
::RuboCop::Cop::AutocorrectLogic - Included
Instance Method Summary
- #any_none_block_intersection(node)
- #bad_intersection_check?(node, predicates)
- #intersection_size_check?(node, predicates)
- #on_block(node) (also: #on_numblock, #on_itblock)
-
#on_csend(node)
Alias for #on_send.
-
#on_itblock(node)
Alias for #on_block.
-
#on_numblock(node)
Alias for #on_block.
- #on_send(node) (also: #on_csend)
- #bad_intersection?(node) ⇒ Boolean private
- #bad_intersection_predicates private
- #register_offense(node, replacement) private
- #straight?(method_name) ⇒ Boolean private
::RuboCop::Cop::Base - Inherited
| #add_global_offense | Adds an offense that has no particular location. |
| #add_offense | Adds an offense on the specified range (or node with an expression) Unless that offense is disabled for this range, a corrector will be yielded to provide the cop the opportunity to autocorrect the offense. |
| #begin_investigation | Called before any investigation. |
| #callbacks_needed, | |
| #cop_config | Configuration Helpers. |
| #cop_name, #excluded_file?, | |
| #external_dependency_checksum | This method should be overridden when a cop’s behavior depends on state that lives outside of these locations: |
| #inspect, | |
| #message | Gets called if no message is specified when calling |
| #name | Alias for Base#cop_name. |
| #offenses, | |
| #on_investigation_end | Called after all on_… |
| #on_new_investigation | Called before all on_… |
| #on_other_file | Called instead of all on_… |
| #parse | There should be very limited reasons for a Cop to do it’s own parsing. |
| #parser_engine, | |
| #ready | Called between investigations. |
| #relevant_file?, | |
| #target_gem_version | Returns a gems locked versions (i.e. |
| #target_rails_version, #target_ruby_version, #annotate, #apply_correction, #attempt_correction, | |
| #callback_argument | Reserved for Cop::Cop. |
| #complete_investigation | Called to complete an investigation. |
| #correct, #current_corrector, | |
| #current_offense_locations | Reserved for Commissioner: |
| #current_offenses, #currently_disabled_lines, #custom_severity, #default_severity, #disable_uncorrectable, #enabled_line?, #file_name_matches_any?, #find_message, #find_severity, #range_for_original, #range_from_node_or_range, | |
| #reset_investigation | Actually private methods. |
| #use_corrector | |
::RuboCop::Cop::AutocorrectLogic - Included
::RuboCop::Cop::IgnoredNode - Included
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Method Details
#any_none_block_intersection(node)
[ GitHub ]# File 'lib/rubocop/cop/style/array_intersect.rb', line 119
def_node_matcher :any_none_block_intersection, <<~PATTERN { (block (call $_receiver ${:any? :none?}) (args (arg _key)) (send $_argument :member? (lvar _key)) ) (numblock (call $_receiver ${:any? :none?}) 1 (send $_argument :member? (lvar :_1)) ) (itblock (call $_receiver ${:any? :none?}) :it (send $_argument :member? (lvar :it)) ) } PATTERN
#bad_intersection?(node) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/array_intersect.rb', line 168
def bad_intersection?(node) bad_intersection_check?(node, bad_intersection_predicates) || intersection_size_check?(node) end
#bad_intersection_check?(node, predicates)
[ GitHub ]# File 'lib/rubocop/cop/style/array_intersect.rb', line 94
def_node_matcher :bad_intersection_check?, <<~PATTERN $(call { (begin (send $_ :& $_)) (call $!nil? :intersection $_) } $%1 ) PATTERN
#bad_intersection_predicates (private)
[ GitHub ]# File 'lib/rubocop/cop/style/array_intersect.rb', line 173
def bad_intersection_predicates if active_support_extensions_enabled? ACTIVE_SUPPORT_PREDICATES else PREDICATES end end
#intersection_size_check?(node, predicates)
[ GitHub ]# File 'lib/rubocop/cop/style/array_intersect.rb', line 105
def_node_matcher :intersection_size_check?, <<~PATTERN (call $(call { (begin (send $_ :& $_)) (call $!nil? :intersection $_) } %ARRAY_SIZE_METHODS ) {$:> (int 0) | $:positive? | $:!= (int 0) | $:== (int 0) | $:zero?} ) PATTERN
#on_block(node) Also known as: #on_numblock, #on_itblock
[ GitHub ]# File 'lib/rubocop/cop/style/array_intersect.rb', line 154
def on_block(node) return unless (receiver, method_name, argument = any_none_block_intersection(node)) dot = node.send_node.loc.dot.source bang = method_name == :any? ? '' : '!' replacement = "#{bang}#{receiver.source}#{dot}intersect?(#{argument.source})" register_offense(node, replacement) end
#on_csend(node)
Alias for #on_send.
# File 'lib/rubocop/cop/style/array_intersect.rb', line 152
alias on_csend on_send
#on_itblock(node)
Alias for #on_block.
# File 'lib/rubocop/cop/style/array_intersect.rb', line 164
alias on_itblock on_block
#on_numblock(node)
Alias for #on_block.
# File 'lib/rubocop/cop/style/array_intersect.rb', line 163
alias on_numblock on_block
#on_send(node) Also known as: #on_csend
[ GitHub ]# File 'lib/rubocop/cop/style/array_intersect.rb', line 142
def on_send(node) return if node.block_literal? return unless (dot_node, receiver, argument, method_name = bad_intersection?(node)) dot = dot_node.loc.dot.source bang = straight?(method_name) ? '' : '!' replacement = "#{bang}#{receiver.source}#{dot}intersect?(#{argument.source})" register_offense(node, replacement) end
#register_offense(node, replacement) (private)
[ GitHub ]# File 'lib/rubocop/cop/style/array_intersect.rb', line 185
def register_offense(node, replacement) = format(MSG, replacement: replacement, existing: node.source) add_offense(node, message: ) do |corrector| corrector.replace(node, replacement) end end
#straight?(method_name) ⇒ Boolean (private)
# File 'lib/rubocop/cop/style/array_intersect.rb', line 181
def straight?(method_name) STRAIGHT_METHODS.include?(method_name.to_sym) end