-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Style/SafeNavigation cop crashes when auto correct #3510
Comments
Additional examples of incorrect auto-correct (duh!)
if a
a.b c
else
'whatever'
end is corrected into if a
a&.b c
# and that's all, rest is simply wiped First correction results in broken logic, second and third corrections result in syntax errors |
Thanks for the bug report. I think that I have an easy fix for the first issue. The other examples will take a little longer to fix, but I don't expect them to be too difficult to fix. |
It looks like github auto-linked up my progress on this change since I reused the branch name. I have a quick solution for @pocke's original request: if foo
foo.bar
end The easiest solution is to not register an offense for non modifier if statements. I would like to spend a little more time on this and register an offense for simple if statement, and not register an offense for more complicated ones. # register an offense for this
if foo
foo.bar
end
# allow
if foo
foo.bar
baz
end
# allow
if foo
foo.bar
else
something
end @dreyks I think that the examples that you provided # this is questionable with safe navigation
def something?
!a || a.empty? # this will always return true or false
end
def something?
a&.empty? # this will return true, false, or nil
end
# this works fine with safe navigation because nil will be treated as false
something if !a || a.empty? |
The thing is if a variable is before def do_something(data = nil)
return if !data || data.empty?
p 'doing stuff'
end
do_something # => does not output anything after def do_something(data = nil)
return if data&.empty?
p 'doing stuff'
end
do_something # => outputs "doing stuff" |
You are correct. I will remove the checks for || and make a configuration for && for the reasons listed before. |
Might be better to cut a release since so many people are reporting the same problem. |
* bbatsov/master: (80 commits) [Fix rubocop#3540] Make `Style/GuardClause` register offense for instance & singleton methods [Fix rubocop#3436] issue related to Rails/SaveBang when returning non-bang call from the parent method Allow `#to_yml` to produce single-quoted strings Add support for StyleGuideBaseURL and update rules Add spec for the existing style guide URL implementation Fix the changelog Edited regular expression for normal case to fix issues rubocop#3514 and rubocop#3516 (rubocop#3524) Add a rake task for generation a new cop (rubocop#3533) [Fix rubocop#3510] Various bug fixes for SafeNavigation (rubocop#3517) [Fix rubocop#3512] Change error message of `Lint/UnneededSplatExpansion` for array (rubocop#3526) Fix false positive in `Lint/AssignmentInCondition` (rubocop#3520) (rubocop#3529) Rename a mismatched filename (rubocop#3523) Fix a broken changelog entry [Fix rubocop#3511] Style/TernaryParentheses false positive (rubocop#3513) Fix the release notes for 0.43 Cut 0.43.0 [Fix rubocop#3462] Don't flag single-line methods Fix false negatives in `Rails/NotNullColumn` cop (rubocop#3508) Remove unused doubled methods (rubocop#3509) [Fix rubocop#3485] Make OneLineConditional cop ignore empty else (rubocop#3487) ...
cc: @rrosenblum
Expected behavior
Don't crash
Actual behavior
RuboCop crashes.
Steps to reproduce the problem
Put the following files.
test.rb
.rubocop.yml
And run RuboCop with auto correction.
RuboCop version
The text was updated successfully, but these errors were encountered: