Lua checker on mlua — undefined variable / global / field detection with LuaCats support.
Designed to run before Lua execution, providing a safety net for AI-driven and programmatic Lua code generation.
Backed by emmylua_code_analysis for accurate Lua 5.4 diagnostics.
- Undefined variable — reference to a variable not defined in any enclosing scope
- Undefined global — reference to a global name not in the known symbol table
- Undefined field — access to a field not declared in a
---@classdefinition - Unused variable — a local variable that is declared but never referenced
- VM introspection — automatically builds symbol table from live
mluaglobals
// third argument: optional search paths prepended to Lua's package.path
let result = mlua_check::run_lint("print('hello')", "@main.lua", &[]).unwrap();
assert_eq!(result.diagnostics.len(), 0);See examples/ for runnable demos (basic_lint, with_vm, luacats).
Run any with cargo run --example <name>.
use mlua::prelude::*;
use mlua_check::register;
let lua = Lua::new();
let alc = lua.create_table().unwrap();
alc.set("llm", lua.create_function(|_, ()| Ok(())).unwrap()).unwrap();
lua.globals().set("alc", alc).unwrap();
// register() introspects the VM and builds a symbol table automatically
let engine = register(&lua).unwrap();
let result = engine.lint("alc.llm('hello')", "@main.lua");
assert_eq!(result.diagnostics.len(), 0);
let result = engine.lint("alc.unknown('hello')", "@main.lua");
assert!(result.warning_count > 0);use mlua_check::{LintConfig, LintPolicy};
let config = LintConfig::default().with_policy(LintPolicy::Strict);| Policy | Behavior |
|---|---|
Strict |
Lint errors block execution |
Warn |
Issues reported, execution proceeds (default) |
Off |
Linting disabled |
RuleId covers the four common categories (UndefinedVariable, UndefinedGlobal, UndefinedField, UnusedVariable) plus Other(String) for any additional codes reported by the analyser. RuleId is #[non_exhaustive], so match expressions require a wildcard arm.
The following issues from the previous walker-based implementation are resolved:
for-inloop variables no longer produce false-positive undefined-global diagnostics.- The Lua 5.4 standard global
argis now recognised without manual declaration. - Custom globals registered on the
mluaVM viaregister(&lua)are injected into the analyser as a---@metavirtual stub, eliminating false positives for host-defined names.
Licensed under either of
at your option.