Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Style/RedundantRegexpEscape can generate incorrect fixes for hyphens in character classes #8335

bpo opened this issue Jul 14, 2020 · 3 comments


Copy link

@bpo bpo commented Jul 14, 2020

RedundantRegexpEscape is meant to detect and remove unneeded escapes for regular expressions. In certain cases an escaped hyphen in the middle of a character class can be automatically unescaped such that its meaning changes from a literal hyphen to creating a range match.

Expected behavior

A hyphen in the middle of a character class should only be unescaped if its resulting meaning stays the same.

Actual behavior

In at least some scenarios the hyphen can be converted to a range meaning. See below for a concrete example.

Steps to reproduce the problem

Example regexp: %r{[:\.\-/]*[^:\.\-/]+[:\.\-/]*}
This regexp could be safely reduced to %r{[:./-]*[^:./-]+[:./-]*} (moving the hyphen to the end of each class to avoid creating a range). Or one could leave the hyphen in place but leave it escaped (only un-escaping the period).
Test string "america:pnw.oregon-portland/kenton"
Using Ruby scan, results are: ["america:", "pnw.", "oregon-", "portland/", "kenton"]

Under this version of Rubocop the regexp above is incorrectly simplified to %r{[:\.-/]*[^:.\-/]+[:\.-/]*}
Ruby scan with this change instead yields: ["america:", "pnw.", "oregon", "portland/", "kenton"]

RuboCop version

$ bundle exec rubocop -V
0.88.0 (using Parser, rubocop-ast 0.1.0, running on ruby 2.6.6 x86_64-darwin19)
@marcandre marcandre added the bug label Jul 14, 2020
Copy link

@marcandre marcandre commented Jul 14, 2020

Thanks for the report 👍

@owst do you feel like checking this out?

owst added a commit to owst/rubocop that referenced this issue Jul 14, 2020

The previous code wouldn't detect character classes that had ":" as
their first element (due to a poor attempt at avoiding issues due to
POSIX bracket expressions). The handling of nested character classes was
also improved to avoid false-negatives.
Copy link

@owst owst commented Jul 14, 2020

Thanks for the heads-up @marcandre! PR opened that will fix this.

Apologies @bpo - this was caused by the leading : in the character classes tripping up some incorrect logic that was attempting to handle POSIX bracket expressions, which has now been simplified/corrected

Copy link

@bpo bpo commented Jul 14, 2020

👍 Nice @owst, thank you for the very handy feature and quick response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.