shard-ameba/src/ameba/rule/lint/rand_zero.cr
Sijawusz Pur Rahnama 07ce595ef2 Readability refactors
2022-12-08 17:47:22 +01:00

42 lines
776 B
Crystal

module Ameba::Rule::Lint
# 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:
#
# ```
# Lint/RandZero:
# Enabled: true
# ```
class RandZero < Base
properties do
description "Disallows rand zero calls"
end
MSG = "%s always returns 0"
def test(source, node : Crystal::Call)
return unless node.name == "rand" &&
node.args.size == 1 &&
(arg = node.args.first).is_a?(Crystal::NumberLiteral) &&
arg.value.in?("0", "1")
issue_for node, MSG % node
end
end
end