diff --git a/lib/rubocop/cop/lint/redundant_safe_navigation.rb b/lib/rubocop/cop/lint/redundant_safe_navigation.rb index be3f0130c7a3..0c49dbe16a9d 100644 --- a/lib/rubocop/cop/lint/redundant_safe_navigation.rb +++ b/lib/rubocop/cop/lint/redundant_safe_navigation.rb @@ -77,10 +77,9 @@ module Lint # class RedundantSafeNavigation < Base include AllowedMethods - include RangeHelp extend AutoCorrector - MSG = 'Redundant safe navigation detected.' + MSG = 'Redundant safe navigation detected, use `.` instead.' MSG_LITERAL = 'Redundant safe navigation with default literal detected.' NIL_SPECIFIC_METHODS = (nil.methods - Object.new.methods).to_set.freeze @@ -111,19 +110,18 @@ def on_csend(node) return if respond_to_nil_specific_method?(node) end - range = range_between(node.loc.dot.begin_pos, node.source_range.end_pos) - add_offense(range) { |corrector| corrector.replace(node.loc.dot, '.') } + range = node.loc.dot + add_offense(range) { |corrector| corrector.replace(range, '.') } end def on_or(node) conversion_with_default?(node) do |send_node| - range = range_between(send_node.loc.dot.begin_pos, node.source_range.end_pos) + range = send_node.loc.dot.begin.join(node.source_range.end) add_offense(range, message: MSG_LITERAL) do |corrector| corrector.replace(send_node.loc.dot, '.') - range_with_default = range_between(node.lhs.source_range.end.begin_pos, - node.source_range.end.end_pos) + range_with_default = node.lhs.source_range.end.begin.join(node.source_range.end) corrector.remove(range_with_default) end end diff --git a/spec/rubocop/cop/lint/redundant_safe_navigation_spec.rb b/spec/rubocop/cop/lint/redundant_safe_navigation_spec.rb index 8e2c7aa5cc6f..c77cec372691 100644 --- a/spec/rubocop/cop/lint/redundant_safe_navigation_spec.rb +++ b/spec/rubocop/cop/lint/redundant_safe_navigation_spec.rb @@ -6,11 +6,11 @@ it 'registers an offense and corrects when `&.` is used for camel case const receiver' do expect_offense(<<~RUBY) Const&.do_something - ^^^^^^^^^^^^^^ Redundant safe navigation detected. + ^^ Redundant safe navigation detected, use `.` instead. ConstName&.do_something - ^^^^^^^^^^^^^^ Redundant safe navigation detected. + ^^ Redundant safe navigation detected, use `.` instead. Const_name&.do_something # It is treated as camel case, similar to the `Naming/ConstantName` cop. - ^^^^^^^^^^^^^^ Redundant safe navigation detected. + ^^ Redundant safe navigation detected, use `.` instead. RUBY expect_correction(<<~RUBY) @@ -30,10 +30,10 @@ it 'registers an offense and corrects when `&.` is used inside `if` condition' do expect_offense(<<~RUBY) if foo&.respond_to?(:bar) - ^^^^^^^^^^^^^^^^^^^ Redundant safe navigation detected. + ^^ Redundant safe navigation detected, use `.` instead. do_something elsif foo&.respond_to?(:baz) - ^^^^^^^^^^^^^^^^^^^ Redundant safe navigation detected. + ^^ Redundant safe navigation detected, use `.` instead. do_something_else end RUBY @@ -50,7 +50,7 @@ it 'registers an offense and corrects when `&.` is used inside `unless` condition' do expect_offense(<<~RUBY) do_something unless foo&.respond_to?(:bar) - ^^^^^^^^^^^^^^^^^^^ Redundant safe navigation detected. + ^^ Redundant safe navigation detected, use `.` instead. RUBY expect_correction(<<~RUBY) @@ -61,7 +61,7 @@ it 'registers an offense and corrects when `&.` is used for string literals' do expect_offense(<<~RUBY) '2012-03-02 16:05:37'&.to_time - ^^^^^^^^^ Redundant safe navigation detected. + ^^ Redundant safe navigation detected, use `.` instead. RUBY expect_correction(<<~RUBY) @@ -72,7 +72,7 @@ it 'registers an offense and corrects when `&.` is used for integer literals' do expect_offense(<<~RUBY) 42&.minutes - ^^^^^^^^^ Redundant safe navigation detected. + ^^ Redundant safe navigation detected, use `.` instead. RUBY expect_correction(<<~RUBY) @@ -83,7 +83,7 @@ it 'registers an offense and corrects when `&.` is used for array literals' do expect_offense(<<~RUBY) [1, 2, 3]&.join(', ') - ^^^^^^^^^^^^ Redundant safe navigation detected. + ^^ Redundant safe navigation detected, use `.` instead. RUBY expect_correction(<<~RUBY) @@ -94,7 +94,7 @@ it 'registers an offense and corrects when `&.` is used for hash literals' do expect_offense(<<~RUBY) {k: :v}&.count - ^^^^^^^ Redundant safe navigation detected. + ^^ Redundant safe navigation detected, use `.` instead. RUBY expect_correction(<<~RUBY) @@ -112,14 +112,14 @@ it 'registers an offense and corrects when `&.` is used inside `#{loop_type}` condition' do expect_offense(<<~RUBY, loop_type: loop_type) %{loop_type} foo&.respond_to?(:bar) - _{loop_type} ^^^^^^^^^^^^^^^^^^^ Redundant safe navigation detected. + _{loop_type} ^^ Redundant safe navigation detected, use `.` instead. do_something end begin do_something end %{loop_type} foo&.respond_to?(:bar) - _{loop_type} ^^^^^^^^^^^^^^^^^^^ Redundant safe navigation detected. + _{loop_type} ^^ Redundant safe navigation detected, use `.` instead. RUBY expect_correction(<<~RUBY) @@ -137,8 +137,8 @@ it 'registers an offense and corrects when `&.` is used inside complex condition' do expect_offense(<<~RUBY) do_something if foo&.respond_to?(:bar) && !foo&.respond_to?(:baz) - ^^^^^^^^^^^^^^^^^^^ Redundant safe navigation detected. - ^^^^^^^^^^^^^^^^^^^ Redundant safe navigation detected. + ^^ Redundant safe navigation detected, use `.` instead. + ^^ Redundant safe navigation detected, use `.` instead. RUBY expect_correction(<<~RUBY)