Skip to content

Commit

Permalink
Merge pull request #9764 from dvandersluis/issue/9762
Browse files Browse the repository at this point in the history
[Fix #9762] Update `VariableForce` to be able to handle `case-match` nodes
  • Loading branch information
koic committed May 3, 2021
2 parents c165e90 + 8f00479 commit d0df484
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/fix_update_variableforce_to_be_able_to.md
@@ -0,0 +1 @@
* [#9762](https://github.com/rubocop/rubocop/issues/9762): Update `VariableForce` to be able to handle `case-match` nodes. ([@dvandersluis][])
15 changes: 15 additions & 0 deletions lib/rubocop/cop/variable_force/branch.rb
Expand Up @@ -226,6 +226,21 @@ def always_run?
end
end

# case target
# in pattern # in_pattern
# else
# else_body
# end
class CaseMatch < Base
define_predicate :target?, child_index: 0
define_predicate :in_pattern?, child_index: 1..-2
define_predicate :else_body?, child_index: -1

def always_run?
target?
end
end

# for element in collection
# loop_body
# end
Expand Down
15 changes: 15 additions & 0 deletions spec/rubocop/cop/lint/useless_assignment_spec.rb
Expand Up @@ -1416,4 +1416,19 @@ def some_method(environment)
end
end
end

context 'inside a `case-match` node', :ruby27 do
it 'does not register an offense when the variable is used' do
expect_no_offenses(<<~RUBY)
case '0'
in String
res = 1
else
res = 2
end
do_something(res)
RUBY
end
end
end

0 comments on commit d0df484

Please sign in to comment.