Skip to content

Error with Performance/Sum inspecting code with a dynamic method name #207

@colinux

Description

@colinux

Rubocop reports an error parsing this simplified code with cop Performance/Sum :

# frozen_string_literal: true

require "ostruct"

ary = [OpenStruct.new(crit1: 1, crit2: 5), OpenStruct.new(crit1: 2, crit2: 7)]

ary.define_singleton_method(:averagize) do |field|
  return unless any?

  local_sum = map(&field).sum # line reported
  (local_sum.to_f / size).round(1)
end

puts ary.averagize(:crit1)
puts ary.averagize(:crit2)
$ bundle exec rubocop -d tmp/debug-perf-sum.rb

For /Users/colin/code/hap: configuration from /Users/colin/code/hap/.rubocop.yml
configuration from /Users/colin/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-performance-1.9.2/config/default.yml
configuration from /Users/colin/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-performance-1.9.2/config/default.yml
Default configuration from /Users/colin/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/config/default.yml
configuration from /Users/colin/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-rails-2.9.1/config/default.yml
configuration from /Users/colin/.rbenv/versions/2.6.6/lib/ruby/gems/2.6.0/gems/rubocop-rails-2.9.1/config/default.yml
Inheriting configuration from /Users/colin/code/hap/.rubocop_todo.yml
Inspecting 1 file
Scanning /Users/colin/code/hap/tmp/debug-perf-sum.rb
An error occurred while Performance/Sum cop was inspecting /Users/colin/code/hap/tmp/debug-perf-sum.rb:10:14.
undefined method `source_range' for nil:NilClass
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-performance-1.9.2/lib/rubocop/cop/performance/sum.rb:231:in `method_call_with_args_range'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-performance-1.9.2/lib/rubocop/cop/performance/sum.rb:147:in `autocorrect_sum_map'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-performance-1.9.2/lib/rubocop/cop/performance/sum.rb:122:in `block (2 levels) in handle_sum_map_candidate'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/cop/base.rb:339:in `correct'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/cop/base.rb:126:in `add_offense'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-performance-1.9.2/lib/rubocop/cop/performance/sum.rb:121:in `block in handle_sum_map_candidate'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-performance-1.9.2/lib/rubocop/cop/performance/sum.rb:91:in `sum_map_candidate?'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-performance-1.9.2/lib/rubocop/cop/performance/sum.rb:116:in `handle_sum_map_candidate'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-performance-1.9.2/lib/rubocop/cop/performance/sum.rb:86:in `on_send'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/cop/commissioner.rb:136:in `public_send'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/cop/commissioner.rb:136:in `block (2 levels) in trigger_restricted_cops'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/cop/commissioner.rb:166:in `with_cop_error_handling'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/cop/commissioner.rb:135:in `block in trigger_restricted_cops'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/cop/commissioner.rb:134:in `each'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/cop/commissioner.rb:134:in `trigger_restricted_cops'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/cop/commissioner.rb:70:in `on_send'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-ast-1.4.0/lib/rubocop/ast/traversal.rb:135:in `on_lvasgn'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/cop/commissioner.rb:71:in `on_lvasgn'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-ast-1.4.0/lib/rubocop/ast/traversal.rb:137:in `block in on_dstr'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-ast-1.4.0/lib/rubocop/ast/traversal.rb:137:in `each'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-ast-1.4.0/lib/rubocop/ast/traversal.rb:137:in `on_dstr'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-ast-1.4.0/lib/rubocop/ast/traversal.rb:158:in `on_block'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/cop/commissioner.rb:71:in `on_block'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-ast-1.4.0/lib/rubocop/ast/traversal.rb:137:in `block in on_dstr'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-ast-1.4.0/lib/rubocop/ast/traversal.rb:137:in `each'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-ast-1.4.0/lib/rubocop/ast/traversal.rb:137:in `on_dstr'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/cop/commissioner.rb:71:in `on_begin'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-ast-1.4.0/lib/rubocop/ast/traversal.rb:20:in `walk'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/cop/commissioner.rb:86:in `investigate'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/cop/team.rb:157:in `investigate_partial'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/cop/team.rb:83:in `investigate'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/runner.rb:315:in `inspect_file'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/runner.rb:259:in `block in do_inspection_loop'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/runner.rb:293:in `block in iterate_until_no_changes'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/runner.rb:286:in `loop'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/runner.rb:286:in `iterate_until_no_changes'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/runner.rb:255:in `do_inspection_loop'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/runner.rb:132:in `block in file_offenses'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/runner.rb:157:in `file_offense_cache'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/runner.rb:131:in `file_offenses'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/runner.rb:122:in `process_file'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/runner.rb:101:in `block in each_inspected_file'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/runner.rb:100:in `each'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/runner.rb:100:in `reduce'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/runner.rb:100:in `each_inspected_file'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/runner.rb:86:in `inspect_files'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/runner.rb:47:in `run'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/cli/command/execute_runner.rb:26:in `block in execute_runner'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/cli/command/execute_runner.rb:52:in `with_redirect'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/cli/command/execute_runner.rb:25:in `execute_runner'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/cli/command/execute_runner.rb:17:in `run'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/cli/command.rb:11:in `run'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/cli/environment.rb:18:in `run'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/cli.rb:65:in `run_command'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/cli.rb:72:in `execute_runners'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/lib/rubocop/cli.rb:41:in `run'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/exe/rubocop:13:in `block in <top (required)>'
<rbenv>/lib/ruby/gems/2.6.0/gems/benchmark-0.1.1/lib/benchmark.rb:308:in `realtime'
<rbenv>/lib/ruby/gems/2.6.0/gems/rubocop-1.8.1/exe/rubocop:12:in `<top (required)>'
<rbenv>/bin/rubocop:23:in `load'
<rbenv>/bin/rubocop:23:in `<top (required)>'
<rbenv>/lib/ruby/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/cli/exec.rb:74:in `load'
<rbenv>/lib/ruby/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/cli/exec.rb:74:in `kernel_load'
<rbenv>/lib/ruby/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/cli/exec.rb:28:in `run'
<rbenv>/lib/ruby/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/cli.rb:463:in `exec'
<rbenv>/lib/ruby/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
<rbenv>/lib/ruby/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
<rbenv>/lib/ruby/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
<rbenv>/lib/ruby/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/cli.rb:27:in `dispatch'
<rbenv>/lib/ruby/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
<rbenv>/lib/ruby/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/cli.rb:18:in `start'
<rbenv>/lib/ruby/gems/2.6.0/gems/bundler-1.17.3/exe/bundle:30:in `block in <top (required)>'
<rbenv>/lib/ruby/gems/2.6.0/gems/bundler-1.17.3/lib/bundler/friendly_errors.rb:124:in `with_friendly_errors'
<rbenv>/lib/ruby/gems/2.6.0/gems/bundler-1.17.3/exe/bundle:22:in `<top (required)>'
./bin/bundle:3:in `load'
./bin/bundle:3:in `<main>'

$ bundle exec rubocop -V
1.8.1 (using Parser 3.0.0.0, rubocop-ast 1.4.0, running on ruby 2.6.6 x86_64-darwin19)
  - rubocop-performance 1.9.2
  - rubocop-rails 2.9.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions