lib/rubocop/cop/lint/redundant_with_object.rb in rubocop-0.88.0 vs lib/rubocop/cop/lint/redundant_with_object.rb in rubocop-0.89.0
- old
+ new
@@ -24,12 +24,13 @@
# # good
# ary.each do |v|
# v
# end
#
- class RedundantWithObject < Cop
+ class RedundantWithObject < Base
include RangeHelp
+ extend AutoCorrector
MSG_EACH_WITH_OBJECT = 'Use `each` instead of `each_with_object`.'
MSG_WITH_OBJECT = 'Remove redundant `with_object`.'
@@ -41,23 +42,19 @@
(arg _))
...)
PATTERN
def on_block(node)
- redundant_with_object?(node) do |send|
- add_offense(node, location: with_object_range(send))
- end
- end
+ return unless (send = redundant_with_object?(node))
- def autocorrect(node)
- lambda do |corrector|
- redundant_with_object?(node) do |send|
- if send.method?(:each_with_object)
- corrector.replace(with_object_range(send), 'each')
- else
- corrector.remove(with_object_range(send))
- corrector.remove(send.loc.dot)
- end
+ range = with_object_range(send)
+
+ add_offense(range, message: message(send)) do |corrector|
+ if send.method?(:each_with_object)
+ corrector.replace(range, 'each')
+ else
+ corrector.remove(range)
+ corrector.remove(send.loc.dot)
end
end
end
private