From 4618790e21b2a8f654398da1c4e5b618a9daa23b Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Thu, 12 Dec 2024 10:50:35 +0100 Subject: [PATCH] Fix `Lint/UnescapedBracketInRegexp` cop failure with invalid multibyte escape This is a followup to #13522. The added regex raises a `ScannerError`, which doesn't inherit from `ParserError`. --- changelog/fix_error_unescaped_bracket_scanner_error.md | 1 + lib/rubocop/cop/lint/unescaped_bracket_in_regexp.rb | 2 +- spec/rubocop/cop/lint/unescaped_bracket_in_regexp_spec.rb | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) create mode 100644 changelog/fix_error_unescaped_bracket_scanner_error.md diff --git a/changelog/fix_error_unescaped_bracket_scanner_error.md b/changelog/fix_error_unescaped_bracket_scanner_error.md new file mode 100644 index 000000000000..39d92dc85db4 --- /dev/null +++ b/changelog/fix_error_unescaped_bracket_scanner_error.md @@ -0,0 +1 @@ +* [#13573](https://github.com/rubocop/rubocop/pull/13573): Fix `Lint/UnescapedBracketInRegexp` cop failure with invalid multibyte escape. ([@earlopain][]) diff --git a/lib/rubocop/cop/lint/unescaped_bracket_in_regexp.rb b/lib/rubocop/cop/lint/unescaped_bracket_in_regexp.rb index c1d45cb8e1f5..db91c6f118b8 100644 --- a/lib/rubocop/cop/lint/unescaped_bracket_in_regexp.rb +++ b/lib/rubocop/cop/lint/unescaped_bracket_in_regexp.rb @@ -58,7 +58,7 @@ def on_send(node) Regexp::Parser.parse(text.value)&.each_expression do |expr| detect_offenses(text, expr) end - rescue Regexp::Parser::ParserError + rescue Regexp::Parser::Error # Upon encountering an invalid regular expression, # we aim to proceed and identify any remaining potential offenses. end diff --git a/spec/rubocop/cop/lint/unescaped_bracket_in_regexp_spec.rb b/spec/rubocop/cop/lint/unescaped_bracket_in_regexp_spec.rb index 95659388e3b6..3f53b1b065e7 100644 --- a/spec/rubocop/cop/lint/unescaped_bracket_in_regexp_spec.rb +++ b/spec/rubocop/cop/lint/unescaped_bracket_in_regexp_spec.rb @@ -231,7 +231,7 @@ end context 'invalid regular expressions' do - %w[+ * {42}].each do |invalid_regexp| + %w[+ * {42} \xff].each do |invalid_regexp| it "does not register an offense for single invalid `/#{invalid_regexp}/` regexp`" do expect_no_offenses(<<~RUBY) Regexp.#{method}("#{invalid_regexp}")