mirror of
https://gitea.invidious.io/iv-org/shard-ameba.git
synced 2024-08-15 00:53:29 +00:00
42 lines
776 B
Crystal
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
|