-
Notifications
You must be signed in to change notification settings - Fork 38
/
not_nil.cr
46 lines (42 loc) · 943 Bytes
/
not_nil.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
module Ameba::Rule::Lint
# This rule is used to identify usages of `not_nil!` calls.
#
# For example, this is considered a code smell:
#
# ```
# names = %w[Alice Bob]
# alice = names.find { |name| name == "Alice" }.not_nil!
# ```
#
# And can be written as this:
#
# ```
# names = %w[Alice Bob]
# alice = names.find { |name| name == "Alice" }
#
# if alice
# # ...
# end
# ```
#
# YAML configuration example:
#
# ```
# Lint/NotNil:
# Enabled: true
# ```
class NotNil < Base
properties do
description "Identifies usage of `not_nil!` calls"
end
MSG = "Avoid using `not_nil!`"
def test(source)
AST::NodeVisitor.new self, source, skip: :macro
end
def test(source, node : Crystal::Call)
return unless node.name == "not_nil!"
return unless node.obj && node.args.empty?
issue_for node, MSG, prefer_name_location: true
end
end
end