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 00000000000..8f0b52c2dda --- /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 b411791a95b..11985e37091 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 1226cf639e9..a11a1c5b003 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