Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upWrong correction in Style/IfUnlessModifier with first-line comment and code after the end block on the same line #8499
Comments
marcandre
mentioned this issue
|
Linking to #8281; I think that WhileUntilModifier takes care of this but not IfUnlessModifier They really need to be refactored... |
|
@marcandre , yeah, I agree but that they need to be updated, but what the expected behavior should be in the case described in this issue? |
|
For sure, produce equivalent code and not loose comments. Probably any comment (but after the last element) prevents an offense. |
…hen first-line comment and code after end block
dmytro-savochkin
linked a pull request that will
close
this issue


I think
Style/IfUnlessModifierdoesn't correctly handle the case when there is a first-line comment (right after theifkeyword) AND some code right after theendkeyword. If you correct this code withrubocop -ait would put the comment right after theendkeyword, before the code that was there previously. This basically makes the code invalid after correction.This probably can be better seen in the example below.
I think in such a case we should stop auto-correction because it's pretty non-deterministic: where would that comment should go? Did it belong to the if-end condition? In this case it should go right after it, before the next code. But that's impossible in a single-line modifier form, unless we use these
/* comments */but we probably wouldn't want to do this, so better not to auto-correct.I can provide a PR if it seems right.
Steps to reproduce the problem
We have file
test.rbwith the following code:We run
rubocop test.rb --only Style/IfUnlessModifier -a.Actual behavior
Expected behavior
Everything stays the same and no offense is given (I think it's the right way):
or somehow it's fixed to
but in this case the comment is in the wrong place, or it's fixed to
but we probably don't want to use this syntax for comments.
RuboCop version
0.89.0 (using Parser 2.7.1.4, rubocop-ast 0.2.0, running on ruby 2.5.1 x86_64-darwin18)