Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a false negative for Rails/SafeNavigation #474

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* [#466](https://github.com/rubocop/rubocop-rails/pull/466): Fix a false positive for `Rails/DynamicFindBy` when not inheriting `ApplicationRecord` and without no receiver. ([@koic][])
* [#147](https://github.com/rubocop/rubocop-rails/issues/147): Fix a false positive for `Rails/HasManyOrHasOneDependent` when specifying default `dependent: nil` strategy. ([@koic][])
* [#137](https://github.com/rubocop/rubocop-rails/issues/137): Make `Rails/HasManyOrHasOneDependent` aware of `readonly?` is `true`. ([@koic][])
* [#474](https://github.com/rubocop/rubocop-rails/pull/474): Fix a false negative for `Rails/SafeNavigation` when using `try!` without receiver. ([@koic][])

### Changes

Expand Down
9 changes: 7 additions & 2 deletions lib/rubocop/cop/rails/safe_navigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class SafeNavigation < Base
RESTRICT_ON_SEND = %i[try try!].freeze

def_node_matcher :try_call, <<~PATTERN
(send !nil? ${:try :try!} $_ ...)
(send _ ${:try :try!} $_ ...)
PATTERN

def on_send(node)
Expand All @@ -64,7 +64,12 @@ def autocorrect(corrector, node)
method_node, *params = *node.arguments
method = method_node.source[1..-1]

range = range_between(node.loc.dot.begin_pos, node.loc.expression.end_pos)
range = if node.receiver
range_between(node.loc.dot.begin_pos, node.loc.expression.end_pos)
else
corrector.insert_before(node, 'self')
node
end

corrector.replace(range, replacement(method, params))
end
Expand Down
6 changes: 2 additions & 4 deletions spec/rubocop/cop/rails/safe_navigation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@
it_behaves_like 'accepts', 'try! with a method stored as a variable',
['bar = :==',
'foo.try!(baz, bar)'].join("\n")

it 'accepts usages of try! without receiver' do
expect_no_offenses('try!(:something)')
end
end

context 'try' do
Expand Down Expand Up @@ -118,6 +114,8 @@
'[1, 2].try!(:join, ",")', '[1, 2]&.join(",")'
it_behaves_like 'autocorrect', 'try! with multiple parameters',
'[1, 2].try!(:join, bar, baz)', '[1, 2]&.join(bar, baz)'
it_behaves_like 'autocorrect', 'try! without receiver',
'try!(:join)', 'self&.join'
it_behaves_like 'autocorrect', 'try! with a block',
['[foo, bar].try!(:map) do |e|',
' e.some_method',
Expand Down