Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
colinux opened this issue Jan 18, 2021 · 0 comments · Fixed by #208
Closed

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

colinux opened this issue Jan 18, 2021 · 0 comments · Fixed by #208
Labels
bug Something isn't working

Comments

@colinux
Copy link

colinux commented Jan 18, 2021

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
@koic koic transferred this issue from rubocop/rubocop Jan 18, 2021
@koic koic added the bug Something isn't working label Jan 18, 2021
koic added a commit to koic/rubocop-performance that referenced this issue Jan 18, 2021
Fixes rubocop#207.

This PR fixes an error for `Performance/Sum` when using
`map(&do_something).sum` without receiver.
koic added a commit to koic/rubocop-performance that referenced this issue Jan 18, 2021
Fixes rubocop#207.

This PR fixes an error for `Performance/Sum` when using
`map(&do_something).sum` without receiver.
koic added a commit to koic/rubocop-performance that referenced this issue Jan 22, 2021
Fixes rubocop#207.

This PR fixes an error for `Performance/Sum` when using
`map(&do_something).sum` without receiver.
@koic koic closed this as completed in #208 Jan 22, 2021
koic added a commit that referenced this issue Jan 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants