From dcab0a6218ad128f16b9eebcf4017897864b4f63 Mon Sep 17 00:00:00 2001 From: Daniel Diekmeier Date: Mon, 10 Jan 2022 18:00:06 +0100 Subject: [PATCH] Use `:ambiguous_regexp` in Ruby 3 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jörg Schiller --- changelog/fix_use_different_symbol_in_ruby_3.md | 1 + lib/rubocop/cop/lint/ambiguous_regexp_literal.rb | 6 +++++- spec/rubocop/cop/lint/ambiguous_regexp_literal_spec.rb | 10 +++++++++- 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 changelog/fix_use_different_symbol_in_ruby_3.md diff --git a/changelog/fix_use_different_symbol_in_ruby_3.md b/changelog/fix_use_different_symbol_in_ruby_3.md new file mode 100644 index 000000000000..8f0b52c2dda7 --- /dev/null +++ b/changelog/fix_use_different_symbol_in_ruby_3.md @@ -0,0 +1 @@ +* [#10353](https://github.com/rubocop/rubocop/pull/10353): Use `:ambiguous_regexp` to detect ambiguous Regexp in Ruby 3. ([@danieldiekmeier][], [@joergschiller][]) diff --git a/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb b/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb index b411791a95b9..11985e370912 100644 --- a/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +++ b/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb @@ -30,7 +30,11 @@ class AmbiguousRegexpLiteral < Base def on_new_investigation processed_source.diagnostics.each do |diagnostic| - next unless diagnostic.reason == :ambiguous_literal + if target_ruby_version >= 3.0 + next unless diagnostic.reason == :ambiguous_regexp + else + next unless diagnostic.reason == :ambiguous_literal + end offense_node = find_offense_node_by(diagnostic) diff --git a/spec/rubocop/cop/lint/ambiguous_regexp_literal_spec.rb b/spec/rubocop/cop/lint/ambiguous_regexp_literal_spec.rb index 1226cf639e91..a11a1c5b003c 100644 --- a/spec/rubocop/cop/lint/ambiguous_regexp_literal_spec.rb +++ b/spec/rubocop/cop/lint/ambiguous_regexp_literal_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true RSpec.describe RuboCop::Cop::Lint::AmbiguousRegexpLiteral, :config do - context 'with a regexp literal in the first argument' do + shared_examples 'with a regexp literal in the first argument' do context 'without parentheses' do it 'registers an offense and corrects when single argument' do expect_offense(<<~RUBY) @@ -178,4 +178,12 @@ class MyTest end end end + + context 'Ruby <= 2.7', :ruby27 do + include_examples 'with a regexp literal in the first argument' + end + + context 'Ruby >= 3.0', :ruby30 do + include_examples 'with a regexp literal in the first argument' + end end