diff --git a/CHANGELOG.md b/CHANGELOG.md index 92b15a1d07a..04a1b0ac779 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ ### Bug fixes * [#8195](https://github.com/rubocop-hq/rubocop/issues/8195): Fix an error for `Style/RedundantFetchBlock` when using `#fetch` with empty block. ([@koic][]) +* [#8193](https://github.com/rubocop-hq/rubocop/issues/8193): Fix a false positive for `Style/RedundantRegexpCharacterClass` when using `[\b]`. ([@owst][]) ## 0.86.0 (2020-06-22) diff --git a/lib/rubocop/cop/style/redundant_regexp_character_class.rb b/lib/rubocop/cop/style/redundant_regexp_character_class.rb index de0f08b5c83..80c605c53bc 100644 --- a/lib/rubocop/cop/style/redundant_regexp_character_class.rb +++ b/lib/rubocop/cop/style/redundant_regexp_character_class.rb @@ -34,7 +34,7 @@ class RedundantRegexpCharacterClass < Cop \[ # Literal [ (?!\#\{) # Not (the start of) an interpolation (?: # Either... - \\. | # Any escaped character + \\[^b] | # Any escaped character except b (which would change behaviour) [^.*+?{}()|$] | # or one that doesn't require escaping outside the character class \\[upP]\{[^}]+\} # or a unicode code-point or property ) diff --git a/spec/rubocop/cop/style/redundant_regexp_character_class_spec.rb b/spec/rubocop/cop/style/redundant_regexp_character_class_spec.rb index b79057b1d18..df1b567c1a9 100644 --- a/spec/rubocop/cop/style/redundant_regexp_character_class_spec.rb +++ b/spec/rubocop/cop/style/redundant_regexp_character_class_spec.rb @@ -121,6 +121,14 @@ end end + context 'with a character class containing an escaped-b' do + # See https://github.com/rubocop-hq/rubocop/issues/8193 for details - in short \b != [\b] - the + # former matches a word boundary, the latter a backspace character. + it 'does not register an offense' do + expect_no_offenses('foo = /[\b]/') + end + end + context 'with a character class containing a character requiring escape outside' do # Not implemented for now, since we would have to escape on auto-correct, and the cop message # would need to be dynamic to not be misleading.