diff --git a/changelog/fix_error_for_lint_unmodified_reduce_accumulator.md b/changelog/fix_error_for_lint_unmodified_reduce_accumulator.md new file mode 100644 index 000000000000..f465bad696b3 --- /dev/null +++ b/changelog/fix_error_for_lint_unmodified_reduce_accumulator.md @@ -0,0 +1 @@ +* [#12997](https://github.com/rubocop/rubocop/pull/12997): Fix an error for `Lint/UnmodifiedReduceAccumulator` when the block is empty. ([@earlopain][]) diff --git a/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb b/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb index 24fa303d4131..a8f9c36807e3 100644 --- a/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +++ b/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb @@ -113,6 +113,7 @@ class UnmodifiedReduceAccumulator < Base PATTERN def on_block(node) + return unless node.body return unless reduce_with_block?(node) return unless node.argument_list.length >= 2 diff --git a/spec/rubocop/cop/lint/unmodified_reduce_accumulator_spec.rb b/spec/rubocop/cop/lint/unmodified_reduce_accumulator_spec.rb index 220eb3956acc..0731b16d551a 100644 --- a/spec/rubocop/cop/lint/unmodified_reduce_accumulator_spec.rb +++ b/spec/rubocop/cop/lint/unmodified_reduce_accumulator_spec.rb @@ -16,6 +16,12 @@ RUBY end + it "does not affect #{method} called with an empty block" do + expect_no_offenses(<<~RUBY) + values.#{method}(:+) { |result, value| } + RUBY + end + context "given a #{method} block" do it 'does not register an offense when returning a literal' do expect_no_offenses(<<~RUBY)