Use :ambiguous_regexp
to detect ambiguous Regexp in Ruby 3
#10353
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
At work, we recently started using Rubocop via Standard, and we still have a long
.rubocop_todo.yml
. Luckily, this helped to uncover a bug in RuboCop! :DAfter upgrading a Rails app to Ruby 3.0 and setting
TargetRubyVersion: 3.0
, I noticed that all todos relating toLint/AmbiguousRegexpLiteral
disappeared. But we hadn't changed the files, and the "bad" code was still in there.Digging further, a colleague and I noticed that this only happened with
TargetRubyVersion: 3.0
, and that the offenses showed up when settingTargetRubyVersion: 2.7
.It took a while, but we finally found this code in the parser: https://github.com/whitequark/parser/blob/e160eeb50bf505453c5a9b1538451d2d62e6144a/lib/parser/lexer.rl#L1571-L1578, added in this commit: https://github.com/whitequark/parser/pull/768/files
It adds a new symbol alongside
:ambiguous_literal
, namely:ambiguous_regexp
.RuboCop only checks for
:ambiguous_literal
, so the warning didn't propagate and was ultimately not displayed.This PR uses the new
:ambiguous_regexp
iftarget_ruby_version >= 3.0
.Thanks for your consideration!
Before submitting the PR make sure the following are checked:
Commit message starts with[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.