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

Support Ruby 2.7's pattern matching for Lint/DuplicateBranch #9930

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
@@ -0,0 +1 @@
* [#9930](https://github.com/rubocop/rubocop/pull/9930): Support Ruby 2.7's pattern matching for `Lint/DuplicateBranch` cop. ([@koic][])
3 changes: 2 additions & 1 deletion lib/rubocop/cop/lint/duplicate_branch.rb
Expand Up @@ -4,7 +4,7 @@ module RuboCop
module Cop
module Lint
# This cop checks that there are no repeated bodies
# within `if/unless`, `case-when` and `rescue` constructs.
# within `if/unless`, `case-when`, `case-in` and `rescue` constructs.
#
# With `IgnoreLiteralBranches: true`, branches are not registered
# as offenses if they return a basic literal value (string, symbol,
Expand Down Expand Up @@ -97,6 +97,7 @@ def on_branching_statement(node)
end
alias on_if on_branching_statement
alias on_case on_branching_statement
alias on_case_match on_branching_statement
alias on_rescue on_branching_statement

private
Expand Down
2 changes: 1 addition & 1 deletion rubocop.gemspec
Expand Up @@ -35,7 +35,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency('rainbow', '>= 2.2.2', '< 4.0')
s.add_runtime_dependency('regexp_parser', '>= 1.8', '< 3.0')
s.add_runtime_dependency('rexml')
s.add_runtime_dependency('rubocop-ast', '>= 1.7.0', '< 2.0')
s.add_runtime_dependency('rubocop-ast', '>= 1.8.0', '< 2.0')
s.add_runtime_dependency('ruby-progressbar', '~> 1.7')
s.add_runtime_dependency('unicode-display_width', '>= 1.4.0', '< 3.0')

Expand Down
32 changes: 31 additions & 1 deletion spec/rubocop/cop/lint/duplicate_branch_spec.rb
Expand Up @@ -65,6 +65,36 @@
end
end

shared_examples_for 'literal case-match allowed' do |type, value|
context "when returning a #{type} in multiple branches", :ruby27 do
it 'allows branches to be duplicated' do
expect_no_offenses(<<~RUBY)
case foo
in x then #{value}
in y then #{value}
else #{value}
end
RUBY
end
end
end

shared_examples_for 'literal case-match disallowed' do |type, value|
context "when returning a #{type} in multiple branches", :ruby27 do
it 'registers an offense' do
expect_offense(<<~RUBY, value: value)
case foo
in x then #{value}
in y then #{value}
^^^^^^^^^^^{value} Duplicate branch body detected.
else #{value}
^^^^ Duplicate branch body detected.
end
RUBY
end
end
end

shared_examples_for 'literal rescue allowed' do |type, value|
context "when returning a #{type} in multiple branches" do
it 'allows branches to be duplicated' do
Expand Down Expand Up @@ -400,7 +430,7 @@
context 'with IgnoreConstantBranches: true' do
let(:cop_config) { { 'IgnoreConstantBranches' => true } }

%w[if case rescue].each do |keyword|
%w[if case case-match rescue].each do |keyword|
context "with `#{keyword}`" do
it_behaves_like "literal #{keyword} allowed", 'constant', 'MY_CONST'

Expand Down