Skip to content

Commit

Permalink
[Fix rubocop#10902] Fix an error for Style/RedundantRegexpEscape st…
Browse files Browse the repository at this point in the history
…ring with invalid byte sequence in UTF-8

Fixed: rubocop#10902
  • Loading branch information
ydah committed Jun 12, 2023
1 parent 664bd71 commit 7c51895
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
@@ -0,0 +1 @@
* [#10902](https://github.com/rubocop/rubocop/issues/10902): Fix an error for `Style/RedundantRegexpEscape` string with invalid byte sequence in UTF-8. ([@ydah][])
3 changes: 2 additions & 1 deletion lib/rubocop/cop/style/redundant_regexp_escape.rb
Expand Up @@ -44,7 +44,8 @@ class RedundantRegexpEscape < Base

def on_regexp(node)
each_escape(node) do |char, index, within_character_class|
next if allowed_escape?(node, char, index, within_character_class)
next if char.valid_encoding? && allowed_escape?(node, char, index,
within_character_class)

location = escape_range_at_index(node, index)

Expand Down
13 changes: 13 additions & 0 deletions spec/rubocop/cop/style/redundant_regexp_escape_spec.rb
Expand Up @@ -617,4 +617,17 @@
RUBY
end
end

context 'with escaping invalid byte sequence in UTF-8' do
it 'registers an offense and corrects' do
expect_offense(<<~'RUBY')
r = /[]/
^^ Redundant escape inside regexp literal
RUBY

expect_correction(<<~RUBY)
r = /[§]/
RUBY
end
end
end

0 comments on commit 7c51895

Please sign in to comment.