35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/pedant/checks/nonsense_comparison.rb', line 35
def check(file, tree)
literals = Set.new [
Nasl::Array,
Nasl::List,
Nasl::Integer,
Nasl::String,
Nasl::Ip
]
comparisons = Set.new [ "==", "!=", "=~", "!~", "><", ">!<", "<", ">", "<=", ">=" ]
tree.all(:Call).each do |call|
next if call.name.ident.name != "isnull"
next if call.name.indexes != []
next if call.args.length != 1
next if not literals.include? call.args.first.expr.class
fail
report(:error, "isnull() is called with a literal, which can never be FALSE.")
report(:error, call.args.first.context(call))
end
tree.all(:Expression).each do |expr|
next if not literals.include? expr.lhs.class
next if not literals.include? expr.rhs.class
next if not comparisons.include? expr.op.to_s
fail
report(:error, "Comparing two literals is always TRUE or FALSE.")
report(:error, expr.op.context(expr))
end
tree.all(:Expression).each do |expr|
next if not comparisons.include? expr.op.to_s
next if not expr.lhs.is_a? Nasl::Lvalue
next if not expr.rhs.is_a? Nasl::Lvalue
xmls = [:lhs, :rhs].map do |side|
expr.send(side).to_xml(Builder::XmlMarkup.new)
end
next if xmls[0] != xmls[1]
fail
report(:error, "Comparing two identical Lvalues. This will always be TRUE.")
report(:error, expr.op.context(expr))
end
end
|