Class: RuboCop::Cop::Style::ReduceToHash

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

Overview

Checks for each_with_object, inject, and reduce calls that build a hash from an enumerable, where to_h with a block could be used instead.

This cop complements Style/HashTransformKeys and Style/HashTransformValues, which handle hash-to-hash transformations with destructured key-value pairs. This cop targets the case where a hash is built from individual elements (non-destructured block parameter).

Examples:

# bad
array.each_with_object({}) { |elem, hash| hash[elem.id] = elem.name }

# bad
array.inject({}) { |hash, elem| hash[elem.id] = elem.name; hash }

# bad
array.reduce({}) { |hash, elem| hash[elem.id] = elem.name; hash }

# bad
array.each_with_object({}) { |elem, hash| hash[elem] = elem.to_s }

# good
array.to_h { |elem| [elem.id, elem.name] }

# good
array.to_h { |elem| [elem, elem.to_s] }

Constant Summary collapse

MSG =
'Use `to_h { ... }` instead of `%<method>s`.'
RESTRICT_ON_SEND =
i[each_with_object inject reduce].freeze

Constants included from RangeHelp

RangeHelp::BYTE_ORDER_MARK, RangeHelp::NOT_GIVEN

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

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?, #message, #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

#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

#each_with_object_to_hash?(node) ⇒ Object

each_with_object({}) { |elem, hash| hash = value }



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rubocop/cop/style/reduce_to_hash.rb', line 52

def_node_matcher :each_with_object_to_hash?, "{\n  (block\n    (call _ :each_with_object (hash))\n    (args (arg _elem) (arg _hash))\n    (send (lvar _hash) :[]= $_key $_value))\n  (numblock\n    (call _ :each_with_object (hash))\n    2\n    (send (lvar :_2) :[]= $_key $_value))\n}\n"

#inject_to_hash?(node) ⇒ Object

inject/reduce({}) { |hash, elem| hash = value; hash }



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

def_node_matcher :inject_to_hash?, "{\n  (block\n    (call _ {:inject :reduce} (hash))\n    (args (arg _hash) (arg _elem))\n    (begin\n      (send (lvar _hash) :[]= $_key $_value)\n      (lvar _hash)))\n  (numblock\n    (call _ {:inject :reduce} (hash))\n    2\n    (begin\n      (send (lvar :_1) :[]= $_key $_value)\n      (lvar :_1)))\n}\n"

#on_send(node) ⇒ Object Also known as: on_csend



84
85
86
87
88
89
# File 'lib/rubocop/cop/style/reduce_to_hash.rb', line 84

def on_send(node)
  block_node = node.block_node
  return unless block_node

  check_offense(node, block_node)
end