-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Add autocorrect for Lint/EmptyConditionalBody
cop.
#10498
Add autocorrect for Lint/EmptyConditionalBody
cop.
#10498
Conversation
The main reason why this cop doesn't have an auto-correct is that it's possible that the condition itself results in some (useful) side-effect. We can't also be sure if the body is not empty due to an oversight (e.g. you forgot to do something). That being said, I'm not opposed to adding an auto-correct here, provided it's marked as unsafe. //cc @rubocop/rubocop-core |
Yeah, it may be preferable for user to check whether missing logic will be added or unnecessary logic will be removed rather than auto-correct. So it may be better to document why this cop don't have auto-correct instead of having auto-correct for this case. |
Thanks for your feedback! |
I don't think a new cop is needed. I'm not opposed to an auto-correct here in principle and we already made an exception for a few lint cops. I'm mostly wondering if we shouldn't disable the proposed auto-correct by default or not. |
Note that removing an empty Empty else seems the only thing that can be safely removed. Empty elsif, when it's last branch, could - assuming there is no side effect in the condition. But anything that is not the last branch, is too dangerous. |
remove_case(corrector, node) | ||
remove_comments(corrector, node) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if utilizing source_range_with_comment
won't allow you to remove that in a single method
@OwlKing FYI - I'll be cutting a new release some time tomorrow, so we'll have to wrap this up today if you want it to make the release. |
I suppose a safe autocorrect for this would be; condition
do_something That would still apply side effects of the Running an unsafe auto-correction (removing the In reality though, I would not expect this violation to be widespread in any codebase. Were there lots in your inherited code, @OwlKing? If not, the value of auto-correction is perhaps lower than for more common violations. |
Hi!
I've recently inherited some really poor quality code and ran rubocop autocorrect on it. I noticed it changed code blocks like this:
to
which is sort of half way there :) So this is my attempt at improving it. I see no reason why we can't just remove those empty conditional blocks.
Curious to see what others think.
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.