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 rubocop auto-correct for the Style/CaseLikeIf cop had some unexpected behaviour when dealing with the ruby match operator in combination with if/elsif statements.
To be more precise, the match data object is not returned anymore after moving the regex condition to a case statement.
Expected behavior
The Style/CaseLikeIf cop takes the =~ match operator into account.
Actual behavior
The Style/CaseLikeIf cop autocorrect changed the behaviour of the code, which lead to faulty results and failing test's.
Steps to reproduce the problem
Before
params[:command] = 'set-disable'
if params[:command] == 'remove'
# do something
elsif %r{^set-(?<status>disable|enable)$} =~ params[:command]
puts status
end
Result disable
After auto-correct
params[:command] = 'set-disable'
case params[:command]
when 'remove'
# do something
when /^set-(?<status>disable|enable)$/
puts status
end
Result NameError (undefined local variable or method 'status' for main:Object)
I think I can come up with something. Actually I wrote the code to fix this actual problem but also want to fix some other edge-cases. Will probably make a PR in a couple of weeks.
Indeed, this is because the only way to get local variables set this way is /literal_regex(?<named>)/ =~ var. If the regexp is stored in a variable or constant and used later with =~ that won't work, or if used with === like here it won't work either. 🤷♂️
The rubocop auto-correct for the Style/CaseLikeIf cop had some unexpected behaviour when dealing with the ruby match operator in combination with if/elsif statements.
To be more precise, the match data object is not returned anymore after moving the regex condition to a
case
statement.Expected behavior
The Style/CaseLikeIf cop takes the
=~
match operator into account.Actual behavior
The Style/CaseLikeIf cop autocorrect changed the behaviour of the code, which lead to faulty results and failing test's.
Steps to reproduce the problem
Before
Result
disable
After auto-correct
Result
NameError (undefined local variable or method 'status' for main:Object)
See also openSUSE/open-build-service@6909ea7
RuboCop version
The text was updated successfully, but these errors were encountered: