Linting

Luau comes with a set of linting passes, that help make sure that the code is correct and consistent. Unlike the type checker, that models the behavior of the code thoroughly and points toward type mismatches that are likely to result in runtime errors, the linter is more opinionated and produces warnings that can often be safely ignored, although it’s recommended to keep the code clean of the warnings.

Linter produces many different types of warnings; many of these are enabled by default, and can be suppressed by declaring --!nolint NAME at the top of the file. In dire situations --!nolint at the top of the file can be used to completely disable all warnings (note that the type checker is still active, and requires a separate --!nocheck declaration).

The rest of this page documents all warnings produced by the linter; each warning has a name and a numeric code, the latter is used when displaying warnings.

UnknownGlobal (1)

By default, variables in Luau are global (this is inherited from Lua 5.x and can’t be changed because of backwards compatibility). This means that typos in identifiers are invisible to the parser, and often break at runtime. For this reason, the linter considers all globals that aren’t part of the builtin global table and aren’t explicitly defined in the script “unknown”:

  1. local displayName = "Roblox"
  2. -- Unknown global 'displaName'
  3. print(displaName)

Note that injecting globals via setfenv can produce this warning in correct code; global injection is incompatible with type checking and has performance implications so we recommend against it and in favor of using require with correctly scoped identifiers.

DeprecatedGlobal (2)

Some global names exist for compatibility but their use is discouraged. This mostly affects globals introduced by Roblox, and since they can have problematic behavior or can break in the future, this warning highlights their uses:

  1. -- Global 'ypcall' is deprecated, use 'pcall' instead
  2. ypcall(function()
  3. print("hello")
  4. end)

GlobalUsedAsLocal (3)

The UnknownGlobal lint can catch typos in globals that are read, but can’t catch them in globals that are assigned to. Because of this, and to discourage the use of globals in general, linter detects cases when a global is only used in one function and can be safely converted to a local variable. Note that in some cases this requires declaring the local variable in the beginning of the function instead of where it’s being assigned to.

  1. local function testFunc(a)
  2. if a < 5 then
  3. -- Global 'b' is only used in the enclosing function; consider changing it to local
  4. b = 1
  5. else
  6. b = 2
  7. end
  8. print(b)
  9. end

LocalShadow (4)

In Luau, it is valid to shadow locals and globals with a local variable, including doing it in the same function. This can result in subtle bugs, since the shadowing may not be obvious to the reader. This warning detects cases where local variables shadow other local variables in the same function, or global variables used in the script; for more cases of detected shadowing see LocalShadowPedantic.

  1. local function foo()
  2. for i=1,10 do
  3. -- Variable 'i' shadows previous declaration at line 2
  4. for i=1,10 do
  5. print(i)
  6. end
  7. end
  8. end

SameLineStatement (5)

Luau doesn’t require the use of semicolons and doesn’t automatically insert them at line breaks. When used wisely this results in code that is easy to read and understand, however it can cause subtle issues and hard to understand code when abused by using many different statements on the same line. This warning highlights cases where code should either be broken into multiple lines, or use ; as a visual guide.

  1. -- A new statement is on the same line; add semi-colon on previous statement to silence
  2. if b < 0 then local a = b + 1 print(a, b) end

MultiLineStatement (6)

An opposite problem is having statements that span multiple lines. This is good for readability when the code is indented properly, but when it’s not it results in code that’s hard to understand, as its easy to confuse the next line for a separate statement.

  1. -- Statement spans multiple lines; use indentation to silence
  2. print(math.max(1,
  3. math.min(2, 3)))

LocalUnused (7)

This warning is one of the few warnings that highlight unused variables. Local variable declarations that aren’t used may indicate a bug in the code (for example, there could be a typo in the code that uses the wrong variable) or redundant code that is no longer necessary (for example, calling a function to get its result and never using this result). This warning warns about locals that aren’t used; if the locals are not used intentionally they can be prefixed with _ to silence the warning:

  1. local x = 1
  2. -- Variable 'y' is never used; prefix with '_' to silence
  3. local y = 2
  4. print(x, x)

FunctionUnused (8)

While unused local variables could be useful for debugging, unused functions usually contain dead code that was meant to be removed. Unused functions clutter code, can be a result of typos similar to local variables, and can mislead the reader into thinking that some functionality is supported.

  1. -- Function 'bar' is never used; prefix with '_' to silence
  2. local function bar()
  3. end
  4. local function foo()
  5. end
  6. return foo()

ImportUnused (9)

In Luau, there’s no first-class module system that’s part of the syntax, but require function acts as an import statement. When a local is initialized with a require result, and the local is unused, this is classified as “unused import”. Removing unused imports improves code quality because it makes it obvious what the dependencies of the code are:

  1. -- Import 'Roact' is never used; prefix with '_' to silence
  2. local Roact = require(game.Packages.Roact)

BuiltinGlobalWrite (10)

While the sandboxing model of Luau prevents overwriting built-in globals such as table for the entire program, it’s still valid to reassign these globals - this results in “global shadowing”, where the script’s global table contains a custom version of table after writing to it. This is problematic because it disables some optimizations, and can result in misleading code. When shadowing built-in globals, use locals instead.

  1. -- Built-in global 'math' is overwritten here; consider using a local or changing the name
  2. math = {}

PlaceholderRead (11)

_ variable name is commonly used as a placeholder to discard function results. The linter follows this convention and doesn’t warn about the use of _ in various cases where a different name would cause a warning otherwise. To make sure the placeholder is only used to write values to it, this warning detects the cases where it’s read instead:

  1. local _ = 5
  2. -- Placeholder value '_' is read here; consider using a named variable
  3. return _

UnreachableCode (12)

In some cases the linter can detect code that is never executed, because all execution paths through the function exit the function or the loop before reaching it. Such code is misleading because it’s not always obvious to the reader that it never runs, and as such it should be removed.

  1. function cbrt(v)
  2. if v >= 0 then
  3. return v ^ (1/3)
  4. else
  5. error('cbrt expects a non-negative argument')
  6. end
  7. -- Unreachable code (previous statement always returns)
  8. return 0
  9. end

UnknownType (13)

Luau provides several functions to get the value type as a string (type, typeof), and some Roblox APIs expose class names through string arguments (Instance.new). This warning detects incorrect use of the type names by checking the string literals used in type comparisons and function calls.

  1. -- Unknown type 'String' (expected primitive type)
  2. if type(v) == "String" then
  3. print("v is a string")
  4. end

ForRange (14)

When using a numeric for, it’s possible to make various mistakes when specifying the for bounds. For example, to iterate through the table backwards, it’s important to specify the negative step size. This warning detects several cases where the numeric for only runs for 0 or 1 iterations, or when the step doesn’t divide the size of the range evenly.

  1. -- For loop should iterate backwards; did you forget to specify -1 as step?
  2. for i=#t,1 do
  3. end

UnbalancedAssignment (15)

Assignment statements and local variable declarations in Luau support multiple variables on the left side and multiple values on the right side. The number of values doesn’t need to match; when the right side has more values, the extra values are discarded, and then the left side has more variables the extra variables are set to nil. However, this can result in subtle bugs where a value is omitted mistakenly. This warning warns about cases like this; if the last expression on the right hand side returns multiple values, the warning is not emitted.

  1. -- Assigning 2 values to 3 variables initializes extra variables with nil; add 'nil' to value list to silence
  2. local x, y, z = 1, 2

ImplicitReturn (16)

In Luau, there’s a subtle difference between returning no values from a function and returning nil. In many contexts these are equivalent, but when the results are passed to a variadic function (perhaps implicitly), the difference can be observed - for example, print(foo()) prints nothing if foo returns no values, and nil if it returns nil.

To help write code that has consistent behavior, linter warns about cases when a function implicitly returns no values, if there are cases when it explicitly returns a result. For code like this it’s recommended to use explicit return or return nil at the end of the function (these have different semantics, so the correct version to use depends on the function):

  1. local function find(t, expected)
  2. for k,v in pairs(t) do
  3. if k == expected then
  4. return v
  5. end
  6. end
  7. -- Function 'find' can implicitly return no values even though there's an explicit return at line 4; add explicit return to silence
  8. end

DuplicateLocal (17)

Luau syntax allows to use the same name for different parameters of a function as well as different local variables declared in the same statement. This is error prone, even if it’s occasionally useful, so a warning is emitted in cases like this, unless the duplicate name is the placeholder _:

  1. function foo(a, b, a) -- Function parameter 'a' already defined on column 14
  2. end
  3. function obj:method(self) -- Function parameter 'self' already defined implicitly
  4. end
  5. local x, y, x = v:GetComponents() -- Variable 'x' already defined on column 7

FormatString (18)

Luau has several library functions that expect a format string that specifies the behavior for the function. These format strings follow a specific syntax that depends on the question; mistakes in these strings can lead to runtime errors or unexpected behavior of the code.

To help make sure that the strings used for these functions are correct, linter checks calls to string.format, string.pack, string.packsize, string.unpack, string.match, string.gmatch, string.find, string.gsub and os.date and issues warnings when the call uses a literal string with an incorrect format:

  1. -- Invalid match pattern: invalid capture reference, must refer to a closed capture
  2. local cap = string.match(s, "(%d)%2")
  3. -- Invalid format string: unfinished format specifier
  4. local str = ("%d %"):format(1, 2)

Note that with the exception of string.format this only works when the function is called via the library, not via the method call (so prefer string.match(s, "pattern") to s:match("pattern")).

TableLiteral (19)

Table literals are often used in Luau to create objects or specify data. The syntax for table literals is rich but invites mistakes, for example it allows duplicate keys or redundant index specification for values already present in the list form. This warning is emitted for cases where some entries in the table literal are ignored at runtime as they’re duplicate:

  1. print({
  2. first = 1,
  3. second = 2,
  4. first = 3, -- Table field 'first' is a duplicate; previously defined at line 2
  5. })

UninitializedLocal (20)

It’s easy to forget to initialize a local variable and then proceed to use it regardless. This can happen due to a typo, or sometimes it can happen because the original variable definition is shadowed. When a local variable doesn’t have an initial value and is used without ever being assigned to, this warning is emitted:

  1. local foo
  2. if foo then -- Variable 'foo' defined at line 1 is never initialized or assigned; initialize with 'nil' to silence
  3. print(foo)
  4. end

DuplicateFunction (21)

This warning is emitted when a script defines two functions with the same name in the same scope.

The warning is not produced when the functions are defined in different scopes because this is much more likely to be intentional.

  1. function foo() end
  2. function foo() end -- Duplicate function definition: 'foo' also defined on line 1
  3. -- OK: the functions are not defined in the same scope.
  4. if x then
  5. function bar() end
  6. else
  7. function bar() end
  8. end

DeprecatedApi (22)

This warning is emitted when a script accesses a method or field that is marked as deprecated. Use of deprecated APIs is discouraged since they may have performance or correctness issues, may result in unexpected behavior, and could be removed in the future.

  1. function getSize(i: Instance)
  2. return i.DataCost -- Member 'Instance.DataCost' is deprecated
  3. end

TableOperations (23)

To manipulate array-like tables, Luau provides a set of functions as part of the standard table library. To help use these functions correctly, this warning is emitted when one of these functions is used incorrectly or can be adjusted to be more performant.

  1. local t = {}
  2. table.insert(t, 0, 42) -- table.insert uses index 0 but arrays are 1-based; did you mean 1 instead?
  3. table.insert(t, #t+1, 42) -- table.insert will append the value to the table; consider removing the second argument for efficiency

DuplicateCondition (24)

When checking multiple conditions via and/or or if/elseif, a copy & paste error may result in checking the same condition redundantly. This almost always indicates a bug, so a warning is emitted when use of a duplicate condition is detected.

  1. assert(self._adorns[normID1] and self._adorns[normID1]) -- Condition has already been checked on column 8

MisleadingAndOr (25)

In Lua, there is no first-class ternary operator but it can be emulated via a and b or c pattern. However, due to how boolean evaluation works, if b is false or nil, the resulting expression evaluates to c regardless of the value of a. Luau solves this problem with the if a then b else c expression; a warning is emitted for and-or expressions where the first alternative is false or nil because it’s almost always a bug.

  1. -- The and-or expression always evaluates to the second alternative because the first alternative is false; consider using if-then-else expression instead
  2. local x = flag and false or true

The code above can be rewritten as follows to avoid the warning and the associated bug:

  1. local x = if flag then false else true

CommentDirective (26)

Luau uses comments that start from ! to control certain aspects of analysis, for example setting type checking mode via --!strict or disabling individual lints with --!nolint. Unknown directives are ignored, for example --!nostrict doesn’t have any effect on the type checking process as the correct spelling is --!nonstrict. This warning flags comment directives that are ignored during processing:

  1. --!nostrict
  2. -- Unknown comment directive 'nostrict'; did you mean 'nonstrict'?"

```