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/UnneededCondition autocorrect changes semantics. #6668
Comments
Given your example, I think we could catch this by checking if the conditions mutate the variable. In reality, I don't think we will be able to catch all instances of this problem. If the modification of the variable happened in a different method, we wouldn't be able to easily identify it. with_retries(max_tries: Dor::Config.release.max_tries, base_sleep_seconds: Dor::Config.release.base_sleep_seconds, max_sleep_seconds: Dor::Config.release.max_sleep_seconds) do |_attempt|
update_member
end
def update_member
@members = @fetcher.get_collection(@druid) # cache members in an instance variable
end Maybe we should mark this cop as unsafe auto-correction. I feel like as time goes on, we are going to find edge cases for nearly every auto-correction, and we are going to wind up marking most auto-correction as unsafe. |
I don't see the autocorrection given above with RuboCop 0.63.0. Instead, I get the following, which seems just fine to me: @members || with_retries(max_tries: Dor::Config.release.max_tries, base_sleep_seconds: Dor::Config.release.base_sleep_seconds, max_sleep_seconds: Dor::Config.release.max_sleep_seconds) do |_attempt|
@members = @fetcher.get_collection(@druid) # cache members in an instance variable
end @jcoyne which version of RuboCop are you using and can you confirm the autocorrection you gave? |
I posted the version in my original report. 0.62.0 |
It still seems to be a problem with 0.63.0 before:
|
Ah, I see what happened when I tried it: I did a full My conclusion: This is a simple bug and UnneededCondition just messes up for the A simpler case: unless bar
foo
else
bar
end Should be corrected to: bar || foo But the actual result is: foo || bar |
Thanks for the investigation @mvz! 🙇 Would you be interested in opening a PR for this? |
@Drenmi I'm trying to wrap my head around the logic of condition, if_branch, else_branch = *node In the present cop, we have both the line above, and TL;DR: I'm going to fix this bug by fixing just this cop, but I'd like to make this |
@Drenmi following up on my last comment, I would like replace unless foo
bar
else
baz
end Here, Wdyt? |
I like the idea behind
def node_parts
if unless?
condition, false_branch, true_branch = *self
else
condition, true_branch, false_branch = *self
end
[condition, true_branch, false_branch]
end |
This certainly seems like an improvement to how understandable the API is. I'm all for it. I'll let you go ahead and decide on the naming. 🙂 |
…utocorrect-with-unless [Fix: #6668] Fix autocorrect for UnneededCondition with unless
Similar to #6415, but that was syntax and this is semantics.
Expected behavior
Autocorrect should not change the behavior of the code.
Actual behavior
Autocorrect changed the behavior of the code.
Steps to reproduce the problem
Given this code:
and I run
rubocop -a
it transforms it into
which does not behave in the same way.
RuboCop version
The text was updated successfully, but these errors were encountered: