A macro based solution could do the trick, so defensive coders can opt-in, here’s a quick one based on Lint.jl:
using Lint
function _noglobal(ex::Expr)
ctx = LintContext()
Lint.lintexpr(ex,ctx)
for m in ctx.messages
m.code == :E321 && error(string(m.message,": ",m.variable))
end
ex
end
macro noglobal(ex)
_noglobal(ex)
end
@noglobal function foo()
x = 1
if x < 1
println("less than 1")
println(bar)
else
println("greater than or equal to 1")
end
end
ERROR: use of undeclared symbol: bar