You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Auto-correct for Style/IfUnlessModifier move any inline comment to the line of the end keyword, it's then removed by the auto-correct for Style/CommentedKeyword
Expected behavior
I think the best way to handle this is to move any inline comment to before the if/unless.
Actual behavior
puts"a line way way way way way way way way way way way way way way way way way way way way too long"iftrue# some comment
gets auto-corrected to:
puts"a line way way way way way way way way way way way way way way way way way way way way too long"iftrue
with the following output from rubocop --auto-correct:
Offenses:
test.rb:1:1: C: [Corrected] Style/IfUnlessModifier: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
iftrue
^^
test.rb:1:104: C: [Corrected] Style/IfUnlessModifier: Modifier form of if makes the line too long.
puts "a line way way way way way way way way way way way way way way way way way way way way too long"iftrue# some comment
^^
test.rb:1:107: W: Lint/LiteralAsCondition: Literal true appeared as a condition.
puts "a line way way way way way way way way way way way way way way way way way way way way too long"iftrue
^^^^
test.rb:3:5: C: [Corrected] Style/CommentedKeyword: Do not place comments on the same line as the end keyword.
end # some comment
^^^^^^^^^^^^^^
1 file inspected, 4 offenses detected, 3 offenses corrected
This is a special case because the corrected line becomes short enough to use if modifier, so it gets corrected back to an inline if.
Steps to reproduce the problem
Run rubocop --auto-correct on:
puts"a line way way way way way way way way way way way way way way way way way way way way too long"iftrue# some comment
The code auto-corrected by Style/IfUnlessModifier is below.
% bundle exec rubocop -a --only Style/IfUnlessModifier(snip)Offenses:example.rb:1:104: C: [Corrected] Style/IfUnlessModifier: Modifier form of if makes the line too long.puts "a line way way way way way way way way way way way way way way way way way way way way too long" if true # some comment ^^1 file inspected, 1 offense detected, 1 offense corrected
% cat example.rbif true puts "a line way way way way way way way way way way way way way way way way way way way way too long"end # some comment
This code is uncommented by Style/CommentedKeyword. This is an expected detection of Style/CommentedKeyword, but this issue showed that auto-correction can remove meaningful comments. RuboCop wouldn't know whether this comment was an auto-correction move or the original comment. So, I opened #9991 to mark Style/CommentedKeyword as unsafe auto-correction. Is was my lack of consideration at #9243.
Auto-correct for Style/IfUnlessModifier move any inline comment to the line of the end keyword, it's then removed by the auto-correct for Style/CommentedKeyword
Expected behavior
I think the best way to handle this is to move any inline comment to before the
if
/unless
.Actual behavior
gets auto-corrected to:
with the following output from
rubocop --auto-correct
:This is a special case because the corrected line becomes short enough to use if modifier, so it gets corrected back to an inline if.
Steps to reproduce the problem
Run
rubocop --auto-correct
on:RuboCop version
The text was updated successfully, but these errors were encountered: