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

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 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:master Aug 5, 2020
@eugeneius
Copy link
Contributor Author

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants