lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb in rubocop-1.4.2 vs lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb in rubocop-1.5.0

- old
+ new

@@ -10,12 +10,11 @@ # If the accumulator is not included in the return value, then the entire # block will just return a transformation of the last element value, and # could be rewritten as such without a loop. # # Also catches instances where an index of the accumulator is returned, as - # this may change the type of object being retained. As well, detects when - # fewer than 2 block arguments are specified. + # this may change the type of object being retained. # # NOTE: For the purpose of reducing false positives, this cop only flags # returns in `reduce` blocks where the element is the only variable in # the expression (since we will not be able to tell what other variables # relate to via static analysis). @@ -66,11 +65,14 @@ class UnmodifiedReduceAccumulator < Base MSG = 'Ensure the accumulator `%<accum>s` will be modified by `%<method>s`.' MSG_INDEX = 'Do not return an element of the accumulator in `%<method>s`.' def_node_matcher :reduce_with_block?, <<~PATTERN - (block (send _recv {:reduce :inject} ...) args ...) + { + (block (send _recv {:reduce :inject} ...) args ...) + (numblock (send _recv {:reduce :inject} ...) ...) + } PATTERN def_node_matcher :accumulator_index?, <<~PATTERN (send (lvar %1) {:[] :[]=} ...) PATTERN @@ -105,14 +107,15 @@ } PATTERN def on_block(node) return unless reduce_with_block?(node) - return unless node.arguments.length >= 2 + return unless node.argument_list.length >= 2 check_return_values(node) end + alias on_numblock on_block private # Return values in a block are either the value given to next, # the last line of a multiline block, or the only line of the block @@ -146,10 +149,10 @@ end end end def block_arg_name(node, index) - node.arguments[index].node_parts[0] + node.argument_list[index].name end # Look for an index of the accumulator being returned, except where the index # is the element. # This is always an offense, in order to try to catch potential exceptions