Skip to content
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

[Fix #9762] Update VariableForce to be able to handle case-match nodes #9764

Merged
merged 1 commit into from May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
dvandersluis marked this conversation as resolved.
Show resolved Hide resolved
expect_no_offenses(<<~RUBY)
case '0'
in String
res = 1
else
res = 2
end

do_something(res)
RUBY
end
end
end