From 7b2e0701c7a89c85d72c846b519bc9a8cee64f22 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Sat, 15 Jun 2024 21:44:40 +0200 Subject: [PATCH] Fix an error for `Lint/UnmodifiedReduceAccumulator` when the block is empty --- .../fix_error_for_lint_unmodified_reduce_accumulator.md | 1 + lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb | 1 + spec/rubocop/cop/lint/unmodified_reduce_accumulator_spec.rb | 6 ++++++ 3 files changed, 8 insertions(+) create mode 100644 changelog/fix_error_for_lint_unmodified_reduce_accumulator.md 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)