Skip to content

Commit

Permalink
Merge pull request rubocop#10353 from danieldiekmeier/fix-ambiguous-r…
Browse files Browse the repository at this point in the history
…egexp-literal-on-ruby-3

Use `:ambiguous_regexp` to detect ambiguous Regexp in Ruby 3
  • Loading branch information
koic committed Jan 11, 2022
2 parents 447294b + dcab0a6 commit c651461
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions 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][])
6 changes: 5 additions & 1 deletion lib/rubocop/cop/lint/ambiguous_regexp_literal.rb
Expand Up @@ -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)

Expand Down
10 changes: 9 additions & 1 deletion 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)
Expand Down Expand Up @@ -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

0 comments on commit c651461

Please sign in to comment.