Rubocop: Error when running `Lint/UnmodifiedReduceAccumulator`

Created on 19 Nov 2020  路  2Comments  路  Source: rubocop-hq/rubocop

Since enabling the Lint/UnmodifiedReduceAccumulator we have found a couple of errors when running against our app. I've disabled the cop in the two places in question but thought I'd raise a bug report to enable you to look into it further if you so choose.


Expected behavior

Running rubocop with the Lint/UnmodifiedReduceAccumulator cop enabled should be successful.

Actual behavior

An error occurred when running the cops see debug output below:

An error occurred while Lint/UnmodifiedReduceAccumulator cop was inspecting /Users/myuser/Projects/safetytoolbox-api/test/integration/get_thing_test.rb:54:14.
undefined method `node_parts' for nil:NilClass
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb:146:in `block_arg_name'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb:131:in `check_return_values'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb:107:in `on_block'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/commissioner.rb:100:in `public_send'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/commissioner.rb:100:in `block (2 levels) in trigger_responding_cops'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/commissioner.rb:166:in `with_cop_error_handling'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/commissioner.rb:99:in `block in trigger_responding_cops'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/commissioner.rb:98:in `each'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/commissioner.rb:98:in `trigger_responding_cops'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/commissioner.rb:69:in `on_block'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:136:in `block in on_dstr'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:136:in `each'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:136:in `on_dstr'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:157:in `on_block'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/commissioner.rb:71:in `on_block'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:136:in `block in on_dstr'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:136:in `each'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:136:in `on_dstr'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:157:in `on_block'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/commissioner.rb:71:in `on_block'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:157:in `on_block'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/commissioner.rb:71:in `on_block'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:136:in `block in on_dstr'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:136:in `each'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:136:in `on_dstr'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:157:in `on_block'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/commissioner.rb:71:in `on_block'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:136:in `block in on_dstr'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:136:in `each'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:136:in `on_dstr'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:157:in `on_block'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/commissioner.rb:71:in `on_block'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:136:in `block in on_dstr'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:136:in `each'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:136:in `on_dstr'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:157:in `on_block'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/commissioner.rb:71:in `on_block'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:136:in `block in on_dstr'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:136:in `each'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:136:in `on_dstr'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:157:in `on_block'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/commissioner.rb:71:in `on_block'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:136:in `block in on_dstr'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:136:in `each'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:136:in `on_dstr'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-ast-1.1.1/lib/rubocop/ast/traversal.rb:20:in `walk'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/commissioner.rb:86:in `investigate'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/team.rb:157:in `investigate_partial'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cop/team.rb:83:in `investigate'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/runner.rb:315:in `inspect_file'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/runner.rb:259:in `block in do_inspection_loop'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/runner.rb:293:in `block in iterate_until_no_changes'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/runner.rb:286:in `loop'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/runner.rb:286:in `iterate_until_no_changes'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/runner.rb:255:in `do_inspection_loop'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/runner.rb:132:in `block in file_offenses'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/runner.rb:157:in `file_offense_cache'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/runner.rb:131:in `file_offenses'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/runner.rb:122:in `process_file'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/runner.rb:101:in `block in each_inspected_file'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/runner.rb:100:in `each'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/runner.rb:100:in `reduce'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/runner.rb:100:in `each_inspected_file'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/runner.rb:86:in `inspect_files'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/runner.rb:47:in `run'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cli/command.rb:11:in `run'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cli/environment.rb:18:in `run'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cli.rb:65:in `run_command'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cli.rb:72:in `execute_runners'
/Users/myuser/.rvm/gems/ruby-2.6.6/gems/rubocop-1.3.1/lib/rubocop/cli.rb:41:in `run'
bin/rubocop:14:in `block in <main>'
/Users/myuser/.rvm/rubies/ruby-2.6.6/lib/ruby/2.6.0/benchmark.rb:308:in `realtime'
bin/rubocop:13:in `<main>'

Steps to reproduce the problem

This occurs in two places where we use Array.new().inject {} in our test suite (see below).

Array.new(string).inject(sequel_model_instance) { |sequel_model_instance| proc_method.(sequel_model_instance) }

The variables (string, sequel_model_instance and proc_method) here are created via minitest spec let, and in these cases we have overwritten the lets inside nested describe blocks. which may be part of the issue.

RuboCop version

$ [bundle exec] rubocop -V
1.3.1 (using Parser 2.7.2.0, rubocop-ast 1.1.1, running on ruby 2.6.6 x86_64-darwin19)
bug

Most helpful comment

@dvandersluis I think my paraphrasing of the line in question was poor (we actually call Array.new(int) but as you point out, a map is much better! Anyway glad I could help weed out an error for you and thank you for fixing so promptly.

All 2 comments

I'm not sure why you're using inject here at all, it seems like you just want to use each or map:

Array.new(string).map { |sequel_model_instance| proc_method.(sequel_model_instance) }

Also Array.new only takes a String or array as the first argument so what is string here?

I'll take a look at what is causing the error though, thanks for the report.

@dvandersluis I think my paraphrasing of the line in question was poor (we actually call Array.new(int) but as you point out, a map is much better! Anyway glad I could help weed out an error for you and thank you for fixing so promptly.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

printercu picture printercu  路  3Comments

tedPen picture tedPen  路  3Comments

bquorning picture bquorning  路  3Comments

millisami picture millisami  路  3Comments

ecbrodie picture ecbrodie  路  3Comments