Today I want to briefly talk about Luacheck. The program performs static analysis on Lua code, acting as a linter. Since my game development consists of code primarily written in Lua this program has quickly become an attractive tool.
Once installed you can run Luacheck on a series of files, or an entire directory and all of its sub-directories. It produces output like so, taken directly from the project page:
$ luacheck src extra_file.lua another_file.lua Checking src/good_code.lua OK Checking src/bad_code.lua Failure src/bad_code.lua:3:23: unused variable length argument src/bad_code.lua:7:10: setting non-standard global variable embrace src/bad_code.lua:8:10: variable opt was previously defined as an argument on line 7 Checking src/python_code.lua Syntax error src/python_code.lua:1:6: expected '=' near '__future__' Checking extra_file.lua Failure extra_file.lua:3:18: unused argument baz extra_file.lua:4:8: unused loop variable i extra_file.lua:13:7: accessing uninitialized variable a extra_file.lua:14:1: value assigned to variable x is unused extra_file.lua:21:7: variable z is never accessed Checking another_file.lua Failure another_file.lua:2:7: unused variable height another_file.lua:3:7: accessing undefined variable heigth Total: 10 warnings / 1 error in 5 files
As you can see, Luacheck not only finds syntax errors—nice for game development to catch those early (as with any software)—but it also warns you on a number of potential problems. From the output above you can see how it catches unused variables, access to uninitialized variables, defining non-standard globals, and so forth. There are a variety of command-line options to control what Luacheck reports but the default settings have proven satisfactory enough in my initial testings.
For Emacs Users
Flycheck is a terrific on-the-fly code checking package for Emacs. It comes with built-in support for Lua that will use Luacheck, if available. So if you’re a developer who uses Emacs, like myself, then enabling Flycheck in your Lua buffers will let you see Luacheck’s warnings and errors in real-time. Very useful.
That’s it. A very brief article, I know. But if you’re a Lua developer and you’ve yet to try Luacheck, give it a moment of your time. Already in my limited use I’ve found it catch mistakes earlier, and that helps me to write better code more efficiently by weeding out those errors quickly.