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

An error occurred while Style/SafeNavigation cop was inspecting file: undefined method 'parent' for nil:NilClass #7171

Closed
snogrammer opened this issue Jun 24, 2019 · 1 comment · Fixed by #7172
Labels

Comments

@snogrammer
Copy link

Expected behavior

Rubocop processes files without any errors encountered.

Actual behavior

Rubocop raises a Style/SafeNavigation error when inspecting file:

undefined method 'parent' for nil:NilClass

Steps to reproduce the problem

# app/lib/core/object.rb snippet causing error

class Object
  # @yield [Object]
  def tap_and_return
    yield self unless nil?
  end
end
rubocop -d

...

Scanning /Users/user/app/lib/core/object.rb
An error occurred while Style/SafeNavigation cop was inspecting /Users/user/app/lib/core/object.rb:32:4.
undefined method `parent' for nil:NilClass
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/style/safe_navigation.rb:227:in `method_called?'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/style/safe_navigation.rb:111:in `use_var_only_in_unless_modifier?'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/style/safe_navigation.rb:101:in `check_node'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/style/safe_navigation.rb:91:in `on_if'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/commissioner.rb:59:in `block (2 levels) in trigger_responding_cops'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/commissioner.rb:130:in `with_cop_error_handling'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/commissioner.rb:58:in `block in trigger_responding_cops'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/commissioner.rb:57:in `each'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/commissioner.rb:57:in `trigger_responding_cops'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/commissioner.rb:34:in `block (2 levels) in <class:Commissioner>'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/ast/traversal.rb:100:in `on_def'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/commissioner.rb:35:in `block (2 levels) in <class:Commissioner>'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/ast/traversal.rb:50:in `block in on_begin'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/ast/traversal.rb:50:in `each'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/ast/traversal.rb:50:in `on_begin'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/commissioner.rb:35:in `block (2 levels) in <class:Commissioner>'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/ast/traversal.rb:92:in `on_class'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/commissioner.rb:35:in `block (2 levels) in <class:Commissioner>'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/ast/traversal.rb:13:in `walk'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/commissioner.rb:46:in `investigate'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/team.rb:116:in `investigate'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/team.rb:104:in `offenses'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/team.rb:44:in `inspect_file'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/runner.rb:289:in `inspect_file'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/runner.rb:233:in `block in do_inspection_loop'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/runner.rb:265:in `block in iterate_until_no_changes'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/runner.rb:258:in `loop'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/runner.rb:258:in `iterate_until_no_changes'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/runner.rb:229:in `do_inspection_loop'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/runner.rb:132:in `block in file_offenses'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/runner.rb:150:in `file_offense_cache'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/runner.rb:130:in `file_offenses'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/runner.rb:118:in `process_file'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/runner.rb:95:in `block in each_inspected_file'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/runner.rb:94:in `each'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/runner.rb:94:in `reduce'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/runner.rb:94:in `each_inspected_file'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/runner.rb:81:in `inspect_files'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/runner.rb:47:in `run'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cli.rb:210:in `execute_runner'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cli.rb:80:in `execute_runners'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cli.rb:51:in `run'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/exe/rubocop:13:in `block in <top (required)>'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/2.6.0/benchmark.rb:308:in `realtime'
/Users/user/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/exe/rubocop:12:in `<top (required)>'
/Users/user/.rbenv/versions/2.6.1/bin/rubocop:23:in `load'
/Users/user/.rbenv/versions/2.6.1/bin/rubocop:23:in `<main>'

RuboCop version

Also happening on 0.70.0

Docker:

0.71.0 (using Parser 2.6.3.0, running on ruby 2.6.1 x86_64-linux-musl)
Finished in 4.798934699996607 seconds
Local:

0.71.0 (using Parser 2.6.3.0, running on ruby 2.6.1 x86_64-darwin18)
Finished in 2.6405830000294372 seconds
@koic koic added the bug label Jun 25, 2019
koic added a commit to koic/rubocop that referenced this issue Jun 25, 2019
Fixes rubocop#7171.

This PR fixes an error for `Style/SafeNavigation` when using `unless nil?`
as a safeguarded.

```ruby
# example.rb
class Object
  # @yield [Object]
  def tap_and_return
    yield self unless nil?
  end
end
```

```console
% rubocop --only Style/SafeNavigation -d
For /private/tmp/7171: configuration from
/Users/koic/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/config/default.yml
Inspecting 1 file
Scanning /private/tmp/7171/example.rb
An error occurred while Style/SafeNavigation cop was inspecting
/private/tmp/7171/example.rb:4:4.
undefined method `parent' for nil:NilClass
/Users/koic/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/style/safe_navigation.rb:227:in
`method_called?'
/Users/koic/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/style/safe_navigation.rb:111:in
`use_var_only_in_unless_modifier?'
```
@koic
Copy link
Member

koic commented Jun 25, 2019

I reproduced it and opened a PR #7172. Thanks!

bbatsov pushed a commit that referenced this issue Jun 25, 2019
Fixes #7171.

This PR fixes an error for `Style/SafeNavigation` when using `unless nil?`
as a safeguarded.

```ruby
# example.rb
class Object
  # @yield [Object]
  def tap_and_return
    yield self unless nil?
  end
end
```

```console
% rubocop --only Style/SafeNavigation -d
For /private/tmp/7171: configuration from
/Users/koic/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/config/default.yml
Inspecting 1 file
Scanning /private/tmp/7171/example.rb
An error occurred while Style/SafeNavigation cop was inspecting
/private/tmp/7171/example.rb:4:4.
undefined method `parent' for nil:NilClass
/Users/koic/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/style/safe_navigation.rb:227:in
`method_called?'
/Users/koic/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/style/safe_navigation.rb:111:in
`use_var_only_in_unless_modifier?'
```
bbatsov pushed a commit that referenced this issue Jun 25, 2019
Fixes #7171.

This PR fixes an error for `Style/SafeNavigation` when using `unless nil?`
as a safeguarded.

```ruby
# example.rb
class Object
  # @yield [Object]
  def tap_and_return
    yield self unless nil?
  end
end
```

```console
% rubocop --only Style/SafeNavigation -d
For /private/tmp/7171: configuration from
/Users/koic/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/config/default.yml
Inspecting 1 file
Scanning /private/tmp/7171/example.rb
An error occurred while Style/SafeNavigation cop was inspecting
/private/tmp/7171/example.rb:4:4.
undefined method `parent' for nil:NilClass
/Users/koic/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/style/safe_navigation.rb:227:in
`method_called?'
/Users/koic/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/rubocop-0.71.0/lib/rubocop/cop/style/safe_navigation.rb:111:in
`use_var_only_in_unless_modifier?'
```
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