Skip to content

Commit

Permalink
Refine offense range for Lint/SafeNavigationWithEmpty
Browse files Browse the repository at this point in the history
This PR refines offense range for `Lint/SafeNavigationWithEmpty`.

The following is an example.

```ruby
return unless foo&.empty?
```

## Before

```console
% bundle exec rubocop example.rb --only Lint/SafeNavigationWithEmpty
(snip)

Offenses:

example.rb:1:1: W: Lint/SafeNavigationWithEmpty: Avoid calling empty?
with the safe navigation operator in conditionals.
return unless foo&.empty?
^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected
```

## After

```console
% bundle exec rubocop example.rb --only Lint/SafeNavigationWithEmpty
(snip)

Offenses:

example.rb:1:15: W: Lint/SafeNavigationWithEmpty: Avoid calling empty?
with the safe navigation operator in conditionals.
return unless foo&.empty?
              ^^^^^^^^^^^

1 file inspected, 1 offense detected
```
  • Loading branch information
koic committed Jun 20, 2020
1 parent 17e4dcd commit 50b145e
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/rubocop/cop/lint/safe_navigation_with_empty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SafeNavigationWithEmpty < Cop
def on_if(node)
return unless safe_navigation_empty_in_conditional?(node)

add_offense(node)
add_offense(node.condition)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cop/lint/safe_navigation_with_empty_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
it 'registers an offense on `&.empty?`' do
expect_offense(<<~RUBY)
return unless foo&.empty?
^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid calling `empty?` with the safe navigation operator in conditionals.
^^^^^^^^^^^ Avoid calling `empty?` with the safe navigation operator in conditionals.
RUBY
end

Expand Down

0 comments on commit 50b145e

Please sign in to comment.