Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
Lint/RedundantSafeNavigation
cop
- Loading branch information
Showing
9 changed files
with
158 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Lint | ||
# This cop checks for redundant safe navigation calls. | ||
# It is marked as unsafe, because it can produce code that returns | ||
# non `nil` while `nil` result is expected on `nil` receiver. | ||
# | ||
# @example | ||
# # bad | ||
# attrs&.respond_to?(:[]) | ||
# foo&.dup&.inspect | ||
# | ||
# # good | ||
# attrs.respond_to?(:[]) | ||
# foo.dup.inspect | ||
# | ||
class RedundantSafeNavigation < Base | ||
include IgnoredMethods | ||
include RangeHelp | ||
extend AutoCorrector | ||
|
||
MSG = 'Redundant safe navigation detected.' | ||
|
||
NIL_METHODS = nil.methods.to_set.freeze | ||
|
||
def on_csend(node) | ||
return unless check_method?(node.method_name) | ||
|
||
range = range_between(node.loc.dot.begin_pos, node.source_range.end_pos) | ||
add_offense(range) do |corrector| | ||
corrector.replace(node.loc.dot, '.') | ||
end | ||
end | ||
|
||
private | ||
|
||
def check_method?(method_name) | ||
NIL_METHODS.include?(method_name) && !ignored_method?(method_name) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Lint::RedundantSafeNavigation, :config do | ||
let(:cop_config) do | ||
{ 'IgnoredMethods' => [] } | ||
end | ||
|
||
it 'registers an offense and corrects when `.&` is used for `nil` method' do | ||
expect_offense(<<~RUBY) | ||
foo&.respond_to?(:bar) | ||
^^^^^^^^^^^^^^^^^^^ Redundant safe navigation detected. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
foo.respond_to?(:bar) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when multiple `.&` are used for `nil` methods' do | ||
expect_offense(<<~RUBY) | ||
foo.do_something&.dup&.inspect | ||
^^^^^ Redundant safe navigation detected. | ||
^^^^^^^^^ Redundant safe navigation detected. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
foo.do_something.dup.inspect | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using non-nil method with `.&`' do | ||
expect_no_offenses(<<~RUBY) | ||
foo.&do_something | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `nil` method without `.&`' do | ||
expect_no_offenses(<<~RUBY) | ||
foo.dup | ||
RUBY | ||
end | ||
|
||
context 'when AllowedMethods is set' do | ||
let(:cop_config) do | ||
{ 'IgnoredMethods' => ['to_f'] } | ||
end | ||
|
||
it 'does not register an offense when using ignored `nil` method with `.&`' do | ||
expect_no_offenses(<<~RUBY) | ||
foo&.to_f | ||
RUBY | ||
end | ||
end | ||
end |