From 50b145e35f3d6321c6a9f62ba952c6c47cb245e0 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 20 Jun 2020 09:28:29 +0900 Subject: [PATCH] Refine offense range for `Lint/SafeNavigationWithEmpty` 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 ``` --- lib/rubocop/cop/lint/safe_navigation_with_empty.rb | 2 +- spec/rubocop/cop/lint/safe_navigation_with_empty_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/rubocop/cop/lint/safe_navigation_with_empty.rb b/lib/rubocop/cop/lint/safe_navigation_with_empty.rb index c4fbfd0168a..530684492ad 100644 --- a/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +++ b/lib/rubocop/cop/lint/safe_navigation_with_empty.rb @@ -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 diff --git a/spec/rubocop/cop/lint/safe_navigation_with_empty_spec.rb b/spec/rubocop/cop/lint/safe_navigation_with_empty_spec.rb index cec0ea6c4cd..2f7688fbc07 100644 --- a/spec/rubocop/cop/lint/safe_navigation_with_empty_spec.rb +++ b/spec/rubocop/cop/lint/safe_navigation_with_empty_spec.rb @@ -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