2018-06-16 11:50:59 +00:00
|
|
|
module Ameba::Rule::Lint
|
2018-03-08 16:55:35 +00:00
|
|
|
# A rule that disallows `rand(0)` and `rand(1)` calls.
|
|
|
|
# Such calls always return `0`.
|
|
|
|
#
|
|
|
|
# For example:
|
|
|
|
#
|
|
|
|
# ```
|
|
|
|
# rand(1)
|
|
|
|
# ```
|
|
|
|
#
|
|
|
|
# Should be written as:
|
|
|
|
#
|
|
|
|
# ```
|
|
|
|
# rand
|
|
|
|
# # or
|
|
|
|
# rand(2)
|
|
|
|
# ```
|
|
|
|
#
|
|
|
|
# YAML configuration example:
|
|
|
|
#
|
|
|
|
# ```
|
2018-09-03 19:53:11 +00:00
|
|
|
# Lint/RandZero:
|
2018-03-08 16:55:35 +00:00
|
|
|
# Enabled: true
|
|
|
|
# ```
|
2021-01-18 15:45:35 +00:00
|
|
|
class RandZero < Base
|
2018-03-08 16:55:35 +00:00
|
|
|
properties do
|
2018-05-03 15:57:47 +00:00
|
|
|
description "Disallows rand zero calls"
|
2018-03-08 16:55:35 +00:00
|
|
|
end
|
|
|
|
|
2018-04-17 04:29:43 +00:00
|
|
|
MSG = "%s always returns 0"
|
|
|
|
|
2018-03-08 16:55:35 +00:00
|
|
|
def test(source, node : Crystal::Call)
|
|
|
|
return unless node.name == "rand" &&
|
|
|
|
node.args.size == 1 &&
|
2022-12-08 13:06:16 +00:00
|
|
|
(arg = node.args.first).is_a?(Crystal::NumberLiteral) &&
|
|
|
|
arg.value.in?("0", "1")
|
2018-03-08 16:55:35 +00:00
|
|
|
|
2018-06-10 21:15:12 +00:00
|
|
|
issue_for node, MSG % node
|
2018-03-08 16:55:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|