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

Use :ambiguous_regexp to detect ambiguous Regexp in Ruby 3 #10353

Merged
merged 1 commit into from
Jan 11, 2022
Merged

Use :ambiguous_regexp to detect ambiguous Regexp in Ruby 3 #10353

merged 1 commit into from
Jan 11, 2022

Conversation

danieldiekmeier
Copy link
Contributor

@danieldiekmeier danieldiekmeier commented Jan 10, 2022

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! :D

After upgrading a Rails app to Ruby 3.0 and setting TargetRubyVersion: 3.0, I noticed that all todos relating to Lint/AmbiguousRegexpLiteral disappeared. But we hadn't changed the files, and the "bad" code was still in there.

- # Offense count: 7
- # Cop supports --auto-correct.
- Lint/AmbiguousRegexpLiteral:
-   Exclude:
-     - 'spec/features/example/example_spec.rb'
-     - 'spec/models/example.rb'
-     - 'spec/views/admin/example/example.haml_spec.rb'

Digging further, a colleague and I noticed that this only happened with TargetRubyVersion: 3.0, and that the offenses showed up when setting TargetRubyVersion: 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 if target_ruby_version >= 3.0.

Thanks for your consideration!


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.

@danieldiekmeier danieldiekmeier marked this pull request as ready for review January 10, 2022 17:18
@danieldiekmeier danieldiekmeier changed the title Use different symbol in ruby 3 Use :ambiguous_regexp to detect ambiguous Regexp in Ruby 3. Jan 10, 2022
@danieldiekmeier danieldiekmeier changed the title Use :ambiguous_regexp to detect ambiguous Regexp in Ruby 3. Use :ambiguous_regexp to detect ambiguous Regexp in Ruby 3 Jan 10, 2022
Co-authored-by: Jörg Schiller <joergschiller@googlemail.com>
@koic koic merged commit c651461 into rubocop:master Jan 11, 2022
@koic
Copy link
Member

koic commented Jan 11, 2022

Good catch! Thank you!

@danieldiekmeier danieldiekmeier deleted the fix-ambiguous-regexp-literal-on-ruby-3 branch January 11, 2022 08:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants