From 118abeb6b086fa788964f3dfaee833f05e534b27 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Wed, 14 Jul 2021 10:06:10 +0900 Subject: [PATCH] Support Ruby 2.7's pattern matching for `Lint/DuplicateBranch` This PR supports Ruby 2.7's pattern matching for `Lint/DuplicateBranch`. And this PR uses rubocop/rubocop-ast#192 feature, so it requires RuboCop AST 1.8.0 or higher. --- ..._doc_and_test_for_lint_duplicate_branch.md | 1 + lib/rubocop/cop/lint/duplicate_branch.rb | 3 +- rubocop.gemspec | 2 +- .../rubocop/cop/lint/duplicate_branch_spec.rb | 32 ++++++++++++++++++- 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 changelog/new_add_pattern_matching_doc_and_test_for_lint_duplicate_branch.md diff --git a/changelog/new_add_pattern_matching_doc_and_test_for_lint_duplicate_branch.md b/changelog/new_add_pattern_matching_doc_and_test_for_lint_duplicate_branch.md new file mode 100644 index 00000000000..a6a6781ee09 --- /dev/null +++ b/changelog/new_add_pattern_matching_doc_and_test_for_lint_duplicate_branch.md @@ -0,0 +1 @@ +* [#9930](https://github.com/rubocop/rubocop/pull/9930): Support Ruby 2.7's pattern matching for `Lint/DuplicateBranch` cop. ([@koic][]) diff --git a/lib/rubocop/cop/lint/duplicate_branch.rb b/lib/rubocop/cop/lint/duplicate_branch.rb index 32d68d0daac..587142b4b03 100644 --- a/lib/rubocop/cop/lint/duplicate_branch.rb +++ b/lib/rubocop/cop/lint/duplicate_branch.rb @@ -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, @@ -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 diff --git a/rubocop.gemspec b/rubocop.gemspec index 032a9e4d571..abb729e9e9f 100644 --- a/rubocop.gemspec +++ b/rubocop.gemspec @@ -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') diff --git a/spec/rubocop/cop/lint/duplicate_branch_spec.rb b/spec/rubocop/cop/lint/duplicate_branch_spec.rb index ccfbada6158..1137c04ad1b 100644 --- a/spec/rubocop/cop/lint/duplicate_branch_spec.rb +++ b/spec/rubocop/cop/lint/duplicate_branch_spec.rb @@ -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 @@ -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'