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

undefined method `source_range' for nil:NilClass in Lint/AmbiguousOperator #7953

Closed
tas50 opened this issue May 11, 2020 · 0 comments · Fixed by #7954
Closed

undefined method `source_range' for nil:NilClass in Lint/AmbiguousOperator #7953

tas50 opened this issue May 11, 2020 · 0 comments · Fixed by #7954
Labels

Comments

@tas50
Copy link
Contributor

tas50 commented May 11, 2020

Hey folks. Thanks for all the work on RuboCop. I ran into this issue testing the engine against our customer code bases.

Expected behavior

RuboCop runs without errors when inspecting:

class Chef::Resource::DiptablesRule < Chef::Resource::LWRPBase
  def validate_diptables_params
    if query
      # some stuff here
    end
  end
end

class Chef::Provider::DiptablesRule < Chef::Provider::LWRPBase
  action :append do
    insert_rule -1
  end
end

Actual behavior

The cop fails with this error:

Scanning /Users/tsmith/dev/the_world/diptables/libraries/base_rule.rb
For /Users/tsmith/dev/the_world/diptables/libraries: An error occurred while Lint/AmbiguousOperator cop was inspecting /Users/tsmith/dev/the_world/diptables/libraries/base_rule.rb.
undefined method `source_range' for nil:NilClass
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/lint/ambiguous_operator.rb:75:in `offense_position?'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/lint/ambiguous_operator.rb:63:in `block in find_offense_node_by'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/ast/node.rb:555:in `block in visit_descendants'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/ast/node.rb:187:in `block in each_child_node'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/ast/node.rb:184:in `each'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/ast/node.rb:184:in `each_child_node'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/ast/node.rb:554:in `visit_descendants'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/ast/node.rb:556:in `block in visit_descendants'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/ast/node.rb:187:in `block in each_child_node'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/ast/node.rb:184:in `each'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/ast/node.rb:184:in `each_child_node'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/ast/node.rb:554:in `visit_descendants'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/ast/node.rb:556:in `block in visit_descendants'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/ast/node.rb:187:in `block in each_child_node'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/ast/node.rb:184:in `each'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/ast/node.rb:184:in `each_child_node'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/ast/node.rb:554:in `visit_descendants'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/ast/node.rb:556:in `block in visit_descendants'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/ast/node.rb:187:in `block in each_child_node'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/ast/node.rb:184:in `each'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/ast/node.rb:184:in `each_child_node'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/ast/node.rb:554:in `visit_descendants'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/ast/node.rb:256:in `each_node'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/lint/ambiguous_operator.rb:62:in `each'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/lint/ambiguous_operator.rb:62:in `find'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/lint/ambiguous_operator.rb:62:in `find_offense_node_by'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/mixin/parser_diagnostic.rb:29:in `block in investigate'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/mixin/parser_diagnostic.rb:20:in `each'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/mixin/parser_diagnostic.rb:20:in `investigate'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/commissioner.rb:108:in `block (2 levels) in invoke_custom_processing'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/commissioner.rb:136:in `with_cop_error_handling'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/commissioner.rb:107:in `block in invoke_custom_processing'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/commissioner.rb:104:in `each'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/commissioner.rb:104:in `invoke_custom_processing'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/commissioner.rb:42:in `investigate'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/team.rb:124:in `investigate'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/team.rb:112:in `offenses'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cop/team.rb:44:in `inspect_file'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:295:in `inspect_file'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:245:in `block in do_inspection_loop'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:277:in `block in iterate_until_no_changes'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:270:in `loop'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:270:in `iterate_until_no_changes'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:241:in `do_inspection_loop'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:122:in `block in file_offenses'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:146:in `file_offense_cache'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:120:in `file_offenses'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:111:in `process_file'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:90:in `block in each_inspected_file'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:89:in `each'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:89:in `reduce'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:89:in `each_inspected_file'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:78:in `inspect_files'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/runner.rb:39:in `run'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cli/command/execute_runner.rb:21:in `execute_runner'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cli/command/execute_runner.rb:13:in `run'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cli/command.rb:10:in `run'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cli/environment.rb:17:in `run'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cli.rb:65:in `run_command'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cli.rb:72:in `execute_runners'
/Users/tsmith/dev/oss/rubocop/lib/rubocop/cli.rb:41:in `run'
/Users/tsmith/dev/oss/rubocop/exe/rubocop:13:in `block in <top (required)>'
/usr/local/Cellar/ruby/2.7.1_2/lib/ruby/2.7.0/benchmark.rb:308:in `realtime'
/Users/tsmith/dev/oss/rubocop/exe/rubocop:12:in `<top (required)>'
/usr/local/lib/ruby/gems/2.7.0/bin/rubocop:23:in `load'
/usr/local/lib/ruby/gems/2.7.0/bin/rubocop:23:in `<top (required)>'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/cli/exec.rb:63:in `load'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/cli/exec.rb:63:in `kernel_load'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/cli/exec.rb:28:in `run'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/cli.rb:476:in `exec'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor.rb:399:in `dispatch'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/cli.rb:30:in `dispatch'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/vendor/thor/lib/thor/base.rb:476:in `start'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/cli.rb:24:in `start'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/exe/bundle:46:in `block in <top (required)>'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/lib/bundler/friendly_errors.rb:123:in `with_friendly_errors'
/usr/local/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/exe/bundle:34:in `<top (required)>'
/usr/local/opt/ruby/bin/bundle:23:in `load'
/usr/local/opt/ruby/bin/bundle:23:in `<main>'

Steps to reproduce the problem

Run rubocop against the above example

RuboCop version

master
@koic koic added the bug label May 12, 2020
koic added a commit to koic/rubocop that referenced this issue May 12, 2020
Fixes rubocop#7953.

This PR fixes the following error for `Lint/AmbiguousOperator`
when a method with no arguments is used in advance.

```console
% cat example.rb
foo
bar +1

% bundle exec rubocop --only Lint/AmbiguousOperator -d
(snip)

An error occurred while Lint/AmbiguousOperator cop was inspecting
/Users/koic/src/github.com/koic/rubocop-issues/7953/example.rb.
undefined method `source_range' for nil:NilClass
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/lint/ambiguous_operator.rb:75:in
`offense_position?'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/lint/ambiguous_operator.rb:63:in
`block in find_offense_node_by'
```
bbatsov pushed a commit that referenced this issue May 12, 2020
Fixes #7953.

This PR fixes the following error for `Lint/AmbiguousOperator`
when a method with no arguments is used in advance.

```console
% cat example.rb
foo
bar +1

% bundle exec rubocop --only Lint/AmbiguousOperator -d
(snip)

An error occurred while Lint/AmbiguousOperator cop was inspecting
/Users/koic/src/github.com/koic/rubocop-issues/7953/example.rb.
undefined method `source_range' for nil:NilClass
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/lint/ambiguous_operator.rb:75:in
`offense_position?'
/Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/lint/ambiguous_operator.rb:63:in
`block in find_offense_node_by'
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants