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/ConditionalAssignment does not report when Metrics/LineLength set to less than 86 #4418
Comments
@rrosenblum Can you look into this? |
Some cops will not suggest a change that would violate the maximum line length. Sounds like this could be one of them. |
Ah, yeah - because by default moving the assignment out makes the first line longer. Guess we should really mention this in the documentation of those cops. Even I forgot about this. :-) |
@Drenmi for the example and replication I have taken the code from the codebase and rubocoped it in its own file. Were you able to replicate it on your side? |
@Drenmi I dont really understand
That does not violate the maximum line length. What am I missing? |
Hi.
more in detail? |
Exactly. That's why I think:
|
Thanks for the feedback on this! |
@DAneas I have been able to replicate this. It looks like there is a bug in the code that is checking for the longest line length. We are double counting the assignment portion so this should have registered an offense.
def do_this_do_that(field, value, filter)
@i_do_not_believe_in_unicorns <<
if filter
{ field => value }
else
{ field => 'i drink bear' }
end
end You are correct that would be short enough. The cop doesn't split the assignment from the condition (maybe this could be advanced behavior to try to register an offense for as much as possible). The cop looks to make the correction in the following way def do_this_do_that(field, value, filter)
@i_do_not_believe_in_unicorns << if filter
{ field => value }
else
{ field => 'i drink bear' }
end
end
I completely agree. I realize that some people may find the correction more important than violating max line length. |
@rrosenblum thanks for taking time to explain this. From
Would it make sense to have |
I have been on the fence about this for some time now. In general, we try to make it so that auto-correction of one cop does not introduce an offense in another cop. Maybe the answer is as you said, always register an offense, but don't automatically fix it. Regardless, it seems like this should at least be configurable and apply consistently to all cops that make use of it. |
…t line is the assignment
Great work @rrosenblum! |
Style/ConditionalAssignment
does not report an offense ifMetrics/LineLength
is set to less than 96.Expected behavior
Changing settings to
Metrics/LineLength
should not affect outcome of other cops.Actual behavior
Offense is not reported when it should be.
Steps to reproduce the problem
Have some code that offends Style/ConditionalAssignment cop. Such as:
Set following rubocop settings:
Outcome:
No offenses
Change settings to (line length more than 95):
Outcome:
Style/ConditionalAssignment
reports an offenseNote:
Specified cops are the only cops that are defined. The rest are defaults
I have noticed that line length changes depending on the code. Initially line lengths were 85 and 86, but after changing to my preferred method names it increased to 95, 96.
RuboCop version
Include the output of
rubocop -V
. Here's an example:The text was updated successfully, but these errors were encountered: