nitc :: DivByZeroVisitor :: defaultinit
# The real visitor that does the real job
private class DivByZeroVisitor
super Visitor
# The toolcontext is our entree point to most services
var toolcontext: ToolContext
# The mmodule is the current module
var mmodule: MModule
redef fun visit(node)
do
# Recursively visit all sub-nodes
node.visit_all(self)
# Now just filter out until we get what we want...
# 1. We need a `/` operation
if not node isa ASlashExpr then return
# 2. The second operand must be an integer literal
var op2 = node.n_expr2
if not op2 isa AIntegerExpr then return
# 3. Its value must be 0
# Note: because of `literal_phase` the `value` method exists
if op2.value != 0 then return
# 4. The receiver (first operand) must be an Int
var op1 = node.n_expr
var int_type = mmodule.get_primitive_class("Int").mclass_type
if not op1.mtype.is_subtype(mmodule, null, int_type) then return
# Error detected
toolcontext.warning(node.location, "div-by-zero", "Warning: division by zero.")
end
end
src/frontend/div_by_zero.nit:49,1--85,3