You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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"]
…scape`
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.
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
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
The text was updated successfully, but these errors were encountered: