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
If an if condition contains a pattern matching with a variable assignment, rubocop may erroneously suggest changing this code to use an if modifier, but the changed code does not work.
Expected behavior
No offences, no autocorrection
Actual behavior
For /home/meh/Projects/sandbox: Default configuration from /home/meh/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/rubocop-1.48.1/config/default.yml
Use parallel by default.
Skipping parallel inspection: only a single file needs inspection
Inspecting 1 file
Scanning /home/meh/Projects/sandbox/rubocop_pattern_match.rb
C
Offenses:
rubocop_pattern_match.rb:3:1: C: [Correctable] Style/IfUnlessModifier: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
if [1] in [x]
^^
1 file inspected, 1 offense detected, 1 offense autocorrectable
Finished in 0.1424515729995619 seconds
Code after autocorrection:
# frozen_string_literal: trueputsxif[1]in[x]
This code does not work because the variables declared to the right of if are not visible to the code to the left of if. As a result, we get an error:
undefined local variable or method `x' for main:Object (NameError)
puts x if [1] in [x]
^
If an
if
condition contains a pattern matching with a variable assignment, rubocop may erroneously suggest changing this code to use anif
modifier, but the changed code does not work.Expected behavior
No offences, no autocorrection
Actual behavior
Code after autocorrection:
This code does not work because the variables declared to the right of if are not visible to the code to the left of if. As a result, we get an error:
Steps to reproduce the problem
RuboCop version
The text was updated successfully, but these errors were encountered: