Acquis 14 - Pedantic Warnings

Contents (8)
  1. Usage
  2. Warnings
    1. W1: Pledge After Seal
    2. W2: pcall/xpcall Deprecation
    3. W3: Moveable Local Declaration
    4. W4: And-Chain for Optional Chaining
    5. W5: Manual Table Deconstruction
  3. Combining with -W

The -Wpedantic flag enables AST-based warnings that identify Lua idioms which can be improved using Lus features. This flag should ideally only be used during development and be avoided in production as it force-enables AST generation for every chunk, which increases memory usage and slows down code generation.

Usage#

lus -Wpedantic script.lus

Warnings are printed to stderr using the standard warning format:

Lua warning: filename:line: message

Warnings#

W1: Pledge After Seal#

Calling pledge() after pledge("seal") has no effect because permissions are frozen.

pledge("fs")
pledge("seal")
pledge("network")  -- Warning: pledge() after pledge("seal") has no effect

W2: pcall/xpcall Deprecation#

References to pcall or xpcall suggest using the catch expression instead (Acquis 1).

local ok, err = pcall(risky)  -- Warning: 'pcall' is deprecated, use 'catch'

Use instead:

local ok, result = catch risky()

W3: Moveable Local Declaration#

Local variables that are only used in the immediately following if or while condition can be moved into the condition using assignment syntax (Acquis 3, Acquis 13).

local x = getValue()
if x then          -- Warning: local declaration can be moved to if condition
    process(x)
end

Use instead:

if x = getValue() then
    process(x)
end

W4: And-Chain for Optional Chaining#

Multi-level and chains checking for nil can use optional chaining (Acquis 2).

local port = cfg and cfg.server and cfg.server.port
-- Warning: and-chain can use optional chaining: x?.y?.z

Use instead:

local port = cfg?.server?.port

This also applies to nested if x ~= nil checks:

if cfg ~= nil then
    if cfg.server ~= nil then
        print(cfg.server.port)
    end
end
-- Warning: nested nil checks can use optional chaining: x?.y?.z

W5: Manual Table Deconstruction#

Pattern of extracting multiple fields from a table can use from syntax (Acquis 4).

local a, b, c = t.a, t.b, t.c
-- Warning: use 'from' destructuring: local ... from t

Use instead:

local a, b, c from t

Combining with -W#

The -Wpedantic flag implies -W (enables runtime warnings), so both compile-time pedantic warnings and runtime warn() calls are active.