Skip to content

Commit

Permalink
Merge pull request #8206 from owst/fix_8205_false_positive_in_redunda…
Browse files Browse the repository at this point in the history
…ntregexpcharacterclass

[Fix #8205] Avoid false positive in Style/RedundantRegexpCharacterClass
  • Loading branch information
koic committed Jun 25, 2020
2 parents e090897 + 8caec83 commit 423323c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,7 @@

* [#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][])
* [#8205](https://github.com/rubocop-hq/rubocop/issues/8205): Fix a false positive for `Style/RedundantRegexpCharacterClass` when using a leading escaped `]`. ([@owst][])

## 0.86.0 (2020-06-22)

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/style/redundant_regexp_character_class.rb
Expand Up @@ -38,6 +38,7 @@ class RedundantRegexpCharacterClass < Cop
[^.*+?{}()|$] | # or one that doesn't require escaping outside the character class
\\[upP]\{[^}]+\} # or a unicode code-point or property
)
(?<!\\) # No \-prefix (i.e. not escaped)
\] # Literal ]
)
/x.freeze
Expand Down
19 changes: 19 additions & 0 deletions spec/rubocop/cop/style/redundant_regexp_character_class_spec.rb
Expand Up @@ -43,6 +43,19 @@
end
end

context 'with a character class containing an escaped [' do
it 'registers an offense and corrects' do
expect_offense(<<~'RUBY')
foo = /[\[]/
^^^^ Redundant single-element character class, `[\[]` can be replaced with `\[`.
RUBY

expect_correction(<<~'RUBY')
foo = /\[/
RUBY
end
end

context 'with a character class containing a space meta-character' do
it 'registers an offense and corrects' do
expect_offense(<<~'RUBY')
Expand Down Expand Up @@ -163,6 +176,12 @@
end
end

context 'with a character class with first element an escaped ]' do
it 'does not register an offense' do
expect_no_offenses('foo = /[\])]/')
end
end

context 'with a negated character class with a single element' do
it 'does not register an offense' do
expect_no_offenses('foo = /[^x]/')
Expand Down

0 comments on commit 423323c

Please sign in to comment.