Skip to content

Commit

Permalink
[Fix #7171] Fix an error for Style/SafeNavigation
Browse files Browse the repository at this point in the history
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?'
```
  • Loading branch information
koic authored and bbatsov committed Jun 25, 2019
1 parent efe9ab4 commit 97ff421
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
## master (unreleased)

### New features
* [#7137](https://github.com/rubocop-hq/rubocop/issues/7137): Add new `Gemspec/RubyVersionGlobalsUsage` cop. ([@malyshkosergey][])

* [#7137](https://github.com/rubocop-hq/rubocop/issues/7137): Add new `Gemspec/RubyVersionGlobalsUsage` cop. ([@malyshkosergey][])
* [#7150](https://github.com/rubocop-hq/rubocop/pull/7150): Add `AllowIfModifier` option to `Style/IfInsideElse` cop. ([@koic][])
* [#7153](https://github.com/rubocop-hq/rubocop/pull/7153): Add new cop `Style/FloatDivision` that checks coercion. ([@tejasbubane][])

Expand All @@ -20,6 +20,7 @@
* [#7151](https://github.com/rubocop-hq/rubocop/issues/7151): Fix `Style/WordArray` to also consider words containing hyphens. ([@fwitzke][])
* [#6893](https://github.com/rubocop-hq/rubocop/issues/6893): Handle implicit rescue correctly in `Naming/RescuedExceptionsVariableName`. ([@pocke][], [@anthony-robin][])
* [#7165](https://github.com/rubocop-hq/rubocop/issues/7165): Fix an auto-correct error for `Style/ConditionalAssignment` when without `else` branch'. ([@koic][])
* [#7171](https://github.com/rubocop-hq/rubocop/issues/7171): Fix an error for `Style/SafeNavigation` when using `unless nil?` as a safeguarded'. ([@koic][])

### Changes

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/safe_navigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def negated?(send_node)
end

def method_called?(send_node)
send_node.parent&.send_type?
send_node&.parent&.send_type?
end

def begin_range(node, method_call)
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/style/safe_navigation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@
expect_no_offenses('foo(bar.baz) if bar')
end

it 'allows a method call safeguarded when using `unless nil?`' do
expect_no_offenses(<<~RUBY)
foo unless nil?
RUBY
end

shared_examples 'all variable types' do |variable|
context 'modifier if' do
shared_examples 'safe guarding logical break keywords' do |keyword|
Expand Down

0 comments on commit 97ff421

Please sign in to comment.