-
-
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 does't understand order of operations #5527
Comments
I ran into a similar problem with the following construct if user && user.thing.blank?
... do stuff
end This cop auto-corrects this to if user&.thing.blank?
... do stuff
end Which is not the same. The first construct does not run the Granted, the statement would probably be better written with a guard clause (which I'll do to avoid rubocop complaining and or fixing it incorrectly). But it seems like this cop might need to be a bit smarter about the order of operations. |
@ptarjan & @bunnymatic this was addressed in #5419. Please test against master and let me know if there is still any issues. We will no longer register an offense for
|
@rrosenblum that's not a safe transformation either. If |
@russell-stripe the example that you provided will not produce a This is an odd issue because it borders on the edge of how is the code explicitly written vs the intention of the code. There was something similar mentioned in another issue that I think you raise a good point, however I think there is a much larger conversation that is needed around this. Should we stop flagging method chains to use safe navigation all together? What would this mean for |
From my perspective, the fix is fine. At some point, if you need to safe navigate that far down into an object, maybe that in itself is a smell and you should revamp the code. I appreciate your looking into this. If nothing else, it forced me to rewrite my code to not be so smelly. So the cops are keeping me on the straight and narrow. Job well done. |
I foresee a lot of false positives on that one. I think the normal case is that the author knows or have ensured that the About |
Thanks for the discussion. I’d like to note that any false positives for
this autocorrect make it impossible for us to apply it to our whole
codebase. Is it possible to have two modes? A sound and simple one and a
more aggressive one with the false positives?
…On Thu, Feb 22, 2018 at 1:32 AM Jonas Arvidsson ***@***.***> wrote:
Should there be a cop that flags user && user.thing.other_thing as code
that could produce a NoMethodError?
I foresee a lot of false positives on that one. I think the normal case is
that the author knows or have ensured that the thing object responds to
other_thing if user is non-nil. It would have to be disabled by default
if we add such a cop.
About user && user.thing.blank?, I think Style/SafeNavigation should
borrow some functionality from Lint/SafeNavigationChain and allow such
constructs when the method call is something that nil responds to (such
as nil?), or one that's in a white list (such as blank?).
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#5527 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AACczwaMqRZVrfRLYBmBSXU0sNsJaGgEks5tXTQxgaJpZM4RxU-b>
.
|
…ject Expressions such as foo && foo.bar.some_method cannot always be changed into code using the safe navigation operator `&.`. If `some_method` is a method that can be called on `nil` (e.g., `nil?`), then we're out of luck. It's because `nil && nil.bar.nil?` returns `nil`, while `nil&.bar.nil?` returns `true`. A related problem is that if there's a chain of methods and the `Style/SafeNavigation` cop converts the first `.` into `&.` (or the user does so, following the cop's advice), then we depend on the `Lint/SafeNavigationChain` to convert the rest of the chain. If `Lint/SafeNavigationChain` is disabled we can't risk making a semantic change, so `Lint/SafeNavigationChain` doesn't report an offense in that situation.
@ptarjan I guess we could have two modes, but I don't think it's necessary. Could you take a look at my PR, which changes |
Expected behavior
This should't be flagged as an error
Actual behavior
Breaks the code with:
Steps to reproduce the problem
RuboCop version
Include the output of
rubocop -V
. Here's an example:The text was updated successfully, but these errors were encountered: