diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f4b7f46f15..318f631d408 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ * [#7705](https://github.com/rubocop-hq/rubocop/issues/7705): Fix `Style/OneLineConditional` cop to handle if/then/elsif/then/else/end cases. Add `AlwaysCorrectToMultiline` config option to this cop to always convert offenses to the multi-line form (false by default). ([@Lykos][], [@dsavochkin][]) * [#8590](https://github.com/rubocop-hq/rubocop/issues/8590): Fix an error when auto-correcting encoding mismatch file. ([@koic][]) * [#8321](https://github.com/rubocop-hq/rubocop/issues/8321): Enable auto-correction for `Layout/{Def}EndAlignment`, `Lint/EmptyEnsure`. ([@marcandre][]) +* [#8583](https://github.com/rubocop-hq/rubocop/issues/8583): Fix `Style/RedundantRegexpEscape` false positive for line continuations. ([@owst][]) ### Changes diff --git a/lib/rubocop/cop/style/redundant_regexp_escape.rb b/lib/rubocop/cop/style/redundant_regexp_escape.rb index 4c370fe613f..0c4d9d9d5a1 100644 --- a/lib/rubocop/cop/style/redundant_regexp_escape.rb +++ b/lib/rubocop/cop/style/redundant_regexp_escape.rb @@ -38,7 +38,7 @@ class RedundantRegexpEscape < Cop MSG_REDUNDANT_ESCAPE = 'Redundant escape inside regexp literal' - ALLOWED_ALWAYS_ESCAPES = ' []^\\#'.chars.freeze + ALLOWED_ALWAYS_ESCAPES = " \n[]^\\#".chars.freeze ALLOWED_WITHIN_CHAR_CLASS_METACHAR_ESCAPES = '-'.chars.freeze ALLOWED_OUTSIDE_CHAR_CLASS_METACHAR_ESCAPES = '.*+?{}()|$'.chars.freeze diff --git a/spec/rubocop/cop/style/redundant_regexp_escape_spec.rb b/spec/rubocop/cop/style/redundant_regexp_escape_spec.rb index f7aacb27eb9..f56596ec2b0 100644 --- a/spec/rubocop/cop/style/redundant_regexp_escape_spec.rb +++ b/spec/rubocop/cop/style/redundant_regexp_escape_spec.rb @@ -16,6 +16,18 @@ end end + context 'with a line continuation' do + it 'does not register an offense' do + expect_no_offenses("foo = /a\\\nb/") + end + end + + context 'with a line continuation within a character class' do + it 'does not register an offense' do + expect_no_offenses("foo = /[a\\\nb]/") + end + end + [ ('a'..'z').to_a - %w[c n p u x], ('A'..'Z').to_a - %w[C M P], @@ -277,6 +289,9 @@ end end + # Avoid an empty character class + next if char == "\n" + context "with an escaped '#{char}' inside a character class" do it 'does not register an offense' do expect_no_offenses("foo = /[\\#{char}]/")