-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
New Lint/RandOne cop #2598
New Lint/RandOne cop #2598
Conversation
PATTERN | ||
|
||
def on_send(node) | ||
rand_argument = rand_call(node) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be rewritten as
rand_call(node) do |rand_argument|
return unless ONES.include?(rand_argument)
add_offense(node, :expression, format(MSG, node.source))
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rrosenblum thank you. I'll update the code.
A couple small tweaks and it looks good to me. I am surprised that |
|
||
def_node_matcher :rand_call, <<-PATTERN | ||
(send {(const nil :Kernel) nil} :rand {(int $_) (float $_)}) | ||
PATTERN |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, you could include the literals here in the pattern. I'm not sure if we support float literals in node patterns... but I could add it if necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alexdowad There are no negative literals (or I didn't manage to find the right syntax). (int -1)
doesn't work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. Do you want negative literals? It's easy to add them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, please :)
On Fri, Jan 8, 2016 at 8:47 AM, Alex Dowad notifications@github.com wrote:
In lib/rubocop/cop/lint/rand_one.rb
#2598 (comment):
#
# @bad
# rand 1
# Kernel.rand(-1)
# rand 1.0
# rand(-1.0)
#
# @good
# 0
class RandOne < Cop
MSG = '`%s` always returns `0`. Perhaps you meant `rand(2)` or `rand`?'
ONES = [1, -1, 1.0, -1.0]
def_node_matcher :rand_call, <<-PATTERN
(send {(const nil :Kernel) nil} :rand {(int $_) (float $_)})
PATTERN
OK. Do you want negative literals? It's easy to add them.
—
Reply to this email directly or view it on GitHub
https://github.com/bbatsov/rubocop/pull/2598/files#r49160027.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See PR #2605.
Me too! I was sure that |
Should probably be in the |
@jawshooah the reason I put it in Performance-wise, |
Thanks to Andrew Grimm for finding this problem.
Rebased on top of #2605 |
It checks for `rand(1.0)` and similar calls that return 0. Such calls most likely show an error in the code. Real life occurrence: Andrew8xx8/kirpich#13 Benchmarks: ```ruby Benchmark.ips do |x| x.report('rand(1)') { rand(1) } x.report('0') { 0 } x.compare! end __END__ Calculating ------------------------------------- rand(1) 48.243k i/100ms 0 60.051k i/100ms ------------------------------------------------- rand(1) 2.066M (± 1.1%) i/s - 10.372M 0 4.608M (± 0.9%) i/s - 23.060M Comparison: 0: 4608288.0 i/s rand(1): 2065579.0 i/s - 2.23x slower ``` Clean up specs Call rand_call with a block
Looks good. |
I wasn't thinking that |
Absolutely. There must be other overhead in the benchmark which is throwing the results out. |
It checks for
rand(1.0)
and similar calls that return 0.Such calls most likely show an error in the code.
I'm not sure if it is really useful. I came across one real life occurrence though: Andrew8xx8/kirpich#13. I wish GitHub search could be better (more like
git grep
) so that I could estimate if it is really a common issue.Benchmarks: