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

Set valid ref range when regexp is matched in Lint/OutOfRangeRegexpRef #8463

Merged

Conversation

@eugeneius
Copy link
Contributor

@eugeneius eugeneius commented Aug 5, 2020

Last match global variables are set when the regexp is matched, not when it's defined. This meant that the previous implementation gave incorrect results when those things happened separately, e.g. when a regexp was assigned to a local variable or a constant and then matched elsewhere.

This change means that the cop only handles matching on regexp literals, but that was the only case where it worked correctly before anyway. It should be possible to improve the cop to handle more complex cases with VariableForce, but that can be done as a separate change.


Before submitting the PR make sure the following are checked:

  • 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.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.
Last match global variables are set when the regexp is matched, not when
it's defined. This meant that the previous implementation gave incorrect
results when those things happened separately, e.g. when a regexp was
assigned to a local variable or a constant and then matched elsewhere.

This change means that the cop only handles matching on regexp literals,
but that was the only case where it worked correctly before anyway. It
should be possible to improve the cop to handle more complex cases with
`VariableForce`, but that can be done as a separate change.
@marcandre
Copy link
Contributor

@marcandre marcandre commented Aug 5, 2020

Agreed, this cop is too eager. I should have thought of that 🤦‍♂️

Would be nice to add the case 'string'.gsub(/.../) { ... } and case str; when /..../ then ....
Do you feel like handling that?

@marcandre marcandre merged commit d0bee92 into rubocop-hq:master Aug 5, 2020
26 checks passed
26 checks passed
windows 2.4
Details
windows 2.5
Details
windows 2.6
Details
windows 2.7
Details
windows mingw
Details
ci/circleci: cc-setup Your tests passed on CircleCI!
Details
ci/circleci: cc-upload-coverage Your tests passed on CircleCI!
Details
ci/circleci: documentation-checks Your tests passed on CircleCI!
Details
ci/circleci: jruby-9.2-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: jruby-9.2-rubocop Your tests passed on CircleCI!
Details
ci/circleci: jruby-9.2-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.4-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.4-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.4-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.5-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.5-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.5-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.6-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.6-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.6-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.7-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.7-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-2.7-spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-head-ascii_spec Your tests passed on CircleCI!
Details
ci/circleci: ruby-head-rubocop Your tests passed on CircleCI!
Details
ci/circleci: ruby-head-spec Your tests passed on CircleCI!
Details
@eugeneius
Copy link
Contributor Author

@eugeneius eugeneius commented Aug 5, 2020

Sure, I'm taking a look now.

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

Successfully merging this pull request may close these issues.

None yet

2 participants
You can’t perform that action at this time.