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
The addition of the safe navigator operator appears to trigger this cop.
However, the fix can break the code.
Passes:
puts([] || {}).length
Fails:
puts([] || {})&.length
Expected behavior
Either leave it as it is:
puts([] || {})&.length
or add extra parenthesis:
puts(([] || {})&.length)
Actual behavior
It removes the space:
puts([] || {})&.length
This can cause the code to return something different. In this example, when I put this line into a method it does not puts anything, whereas before auto-correct it would print 0 to the console.
Steps to reproduce the problem
classTest# rubocop does not mind this. Running the method will print `0`defself.aputs([] || {}).lengthend# rubocop objects to this. Running the method will print `0`defself.bputs([] || {})&.lengthend# rubocop autocorrects to this. Running the method will not print anythingdefself.cputs([] || {})&.lengthend# alternative fix that will print `0`defself.dputs(([] || {})&.length)endend
Use this code & run rubocop against it. You should find the behaviour matches the comments.
…edExpression`
Fixesrubocop#10719 and follow up rubocop#8039.
This PR fixes a false positive for `Lint/ParenthesesAsGroupedExpression`
when using safe navigation operator.
The addition of the safe navigator operator appears to trigger this cop.
However, the fix can break the code.
Passes:
Fails:
Expected behavior
Either leave it as it is:
or add extra parenthesis:
Actual behavior
It removes the space:
This can cause the code to return something different. In this example, when I put this line into a method it does not
puts
anything, whereas before auto-correct it would print0
to the console.Steps to reproduce the problem
Use this code & run rubocop against it. You should find the behaviour matches the comments.
RuboCop version
The text was updated successfully, but these errors were encountered: