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

Ignore non-integer ranges in Style/RandomWithOffset #8754

Merged
merged 1 commit into from Sep 21, 2020

Conversation

eugeneius
Copy link
Contributor

This cop only works correctly for ranges with literal integer bounds.


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.

Copy link
Contributor

@marcandre marcandre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if it wouldn't be better to mark this cop as unsafe instead of restricting it like this.
I'm a bit confused as to which cases are actually problematic...

@@ -36,15 +36,15 @@ class RandomWithOffset < Base
(send
{nil? (const {nil? cbase} :Random) (const {nil? cbase} :Kernel)}
:rand
{int irange erange}))
{int (irange (int _) (int _)) (erange (int _) (int _))}))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No biggie, but (int _) => int is simpler

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, updated!

@eugeneius
Copy link
Contributor Author

The previous behaviour was a crash, so I don't think marking as unsafe will cut it:

$ rubocop _0.91.0_ --debug --only Style/RandomWithOffset --stdin foo.rb <<< "rand(0..limit) + 1"
For /Users/eugene/test: Default configuration from /Users/eugene/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rubocop-0.91.0/config/default.yml
Inspecting 1 file
Scanning /Users/eugene/test/foo.rb
An error occurred while Style/RandomWithOffset cop was inspecting /Users/eugene/test/foo.rb:1:0.
undefined method `+' for nil:NilClass
/Users/eugene/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rubocop-0.91.0/lib/rubocop/cop/style/random_with_offset.rb:112:in `block in corrected_rand_op_integer'
/Users/eugene/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rubocop-0.91.0/lib/rubocop/cop/style/random_with_offset.rb:95:in `random_call'
/Users/eugene/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rubocop-0.91.0/lib/rubocop/cop/style/random_with_offset.rb:105:in `corrected_rand_op_integer'
/Users/eugene/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rubocop-0.91.0/lib/rubocop/cop/style/random_with_offset.rb:83:in `autocorrect'
/Users/eugene/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rubocop-0.91.0/lib/rubocop/cop/style/random_with_offset.rb:68:in `block in on_send'

This happens because the range bounds are assumed to be integer literals here later:

https://github.com/rubocop-hq/rubocop/blob/760d98ba562de63f66cfd789b77d065faf9e664f/lib/rubocop/cop/style/random_with_offset.rb#L147-L149

It might be possible to extend the cop to accept non-integer bounds (perhaps unsafely), but I think that can be a followup.

This cop only works correctly for ranges with integer bounds.
@marcandre marcandre merged commit 058d541 into rubocop:master Sep 21, 2020
@marcandre
Copy link
Contributor

Cool, thanks!

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