.\" Man page generated from reStructuredText. . .TH "LUACHECK" "1" "Sep 24, 2020" "0.24.0" "luacheck" .SH NAME luacheck \- luacheck Documentation . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .sp Contents: .SH LIST OF WARNINGS .sp Warnings produced by Luacheck are categorized using three\-digit warning codes. Warning codes can be displayed in CLI output using \fB\-\-codes\fP CLI option or \fBcodes\fP config option. Errors also have codes starting with zero; unlike warnings, they can not be ignored. .TS center; |l|l|. _ T{ Code T} T{ Description T} _ T{ 011 T} T{ A syntax error. T} _ T{ 021 T} T{ An invalid inline option. T} _ T{ 022 T} T{ An unpaired inline push directive. T} _ T{ 023 T} T{ An unpaired inline pop directive. T} _ T{ 111 T} T{ Setting an undefined global variable. T} _ T{ 112 T} T{ Mutating an undefined global variable. T} _ T{ 113 T} T{ Accessing an undefined global variable. T} _ T{ 121 T} T{ Setting a read\-only global variable. T} _ T{ 122 T} T{ Setting a read\-only field of a global variable. T} _ T{ 131 T} T{ Unused implicitly defined global variable. T} _ T{ 142 T} T{ Setting an undefined field of a global variable. T} _ T{ 143 T} T{ Accessing an undefined field of a global variable. T} _ T{ 211 T} T{ Unused local variable. T} _ T{ 212 T} T{ Unused argument. T} _ T{ 213 T} T{ Unused loop variable. T} _ T{ 221 T} T{ Local variable is accessed but never set. T} _ T{ 231 T} T{ Local variable is set but never accessed. T} _ T{ 232 T} T{ An argument is set but never accessed. T} _ T{ 233 T} T{ Loop variable is set but never accessed. T} _ T{ 241 T} T{ Local variable is mutated but never accessed. T} _ T{ 311 T} T{ Value assigned to a local variable is unused. T} _ T{ 312 T} T{ Value of an argument is unused. T} _ T{ 313 T} T{ Value of a loop variable is unused. T} _ T{ 314 T} T{ Value of a field in a table literal is unused. T} _ T{ 321 T} T{ Accessing uninitialized local variable. T} _ T{ 331 T} T{ Value assigned to a local variable is mutated but never accessed. T} _ T{ 341 T} T{ Mutating uninitialized local variable. T} _ T{ 411 T} T{ Redefining a local variable. T} _ T{ 412 T} T{ Redefining an argument. T} _ T{ 413 T} T{ Redefining a loop variable. T} _ T{ 421 T} T{ Shadowing a local variable. T} _ T{ 422 T} T{ Shadowing an argument. T} _ T{ 423 T} T{ Shadowing a loop variable. T} _ T{ 431 T} T{ Shadowing an upvalue. T} _ T{ 432 T} T{ Shadowing an upvalue argument. T} _ T{ 433 T} T{ Shadowing an upvalue loop variable. T} _ T{ 511 T} T{ Unreachable code. T} _ T{ 512 T} T{ Loop can be executed at most once. T} _ T{ 521 T} T{ Unused label. T} _ T{ 531 T} T{ Left\-hand side of an assignment is too short. T} _ T{ 532 T} T{ Left\-hand side of an assignment is too long. T} _ T{ 541 T} T{ An empty \fBdo\fP \fBend\fP block. T} _ T{ 542 T} T{ An empty \fBif\fP branch. T} _ T{ 551 T} T{ An empty statement. T} _ T{ 561 T} T{ Cyclomatic complexity of a function is too high. T} _ T{ 571 T} T{ A numeric for loop goes from #(expr) down to 1 or less without negative step. T} _ T{ 611 T} T{ A line consists of nothing but whitespace. T} _ T{ 612 T} T{ A line contains trailing whitespace. T} _ T{ 613 T} T{ Trailing whitespace in a string. T} _ T{ 614 T} T{ Trailing whitespace in a comment. T} _ T{ 621 T} T{ Inconsistent indentation (\fBSPACE\fP followed by \fBTAB\fP). T} _ T{ 631 T} T{ Line is too long. T} _ .TE .SS Global variables (1xx) .sp For each file, Luacheck builds list of defined globals and fields which can be used there. By default only globals from Lua standard library are defined; custom globals can be added using \fB\-\-globals\fP CLI option or \fBglobals\fP config option, and version of standard library can be selected using \fB\-\-std\fP CLI option or \fBstd\fP config option. When an undefined global or field is set, mutated or accessed, Luacheck produces a warning. .SS Read\-only globals .sp By default, most standard globals and fields are marked as read\-only, so that setting them produces a warning. Custom read\-only globals and fields can be added using \fB\-\-read\-globals\fP CLI option or \fBread_globals\fP config option, or using a custom set of globals. See custom_stds .sp Globals and fields that are not read\-only by default: .INDENT 0.0 .IP \(bu 2 \fB_G\fP .IP \(bu 2 \fB_ENV\fP (treated as a global by Luacheck) .IP \(bu 2 \fBpackage.path\fP .IP \(bu 2 \fBpackage.cpath\fP .IP \(bu 2 \fBpackage.loaded\fP .IP \(bu 2 \fBpackage.preload\fP .IP \(bu 2 \fBpackage.loaders\fP .IP \(bu 2 \fBpackage.searchers\fP .UNINDENT .SS Implicitly defined globals .sp Luacheck can be configured to consider globals assigned under some conditions to be defined implicitly. When \fB\-d\fP/\fB\-\-allow_defined\fP CLI option or \fBallow_defined\fP config option is used, all assignments to globals define them; when \fB\-t\fP/\fB\-\-allow_defined_top\fP CLI option or \fBallow_defined_top\fP config option is used, assignments to globals in the top level function scope (also known as main chunk) define them. A warning is produced when an implicitly defined global is not accessed anywhere. .SS Modules .sp Files can be marked as modules using \fB\-m\fP/\fB\-\-module\fP CLI option or \fBmodule\fP config option to simulate semantics of the deprecated \fI\%module\fP function. Globals implicitly defined inside a module are considired part of its interface, are not visible outside and are not reported as unused. Assignments to other globals are not allowed, even to defined ones. .SS Unused variables (2xx) and values (3xx) .sp Luacheck generates warnings for all unused local variables except one named \fB_\fP\&. It also detects variables which are set but never accessed or accessed but never set. .SS Unused values and uninitialized variables .sp For each value assigned to a local variable, Luacheck computes set of expressions where it could be used. Warnings are produced for unused values (when a value can\(aqt be used anywhere) and for accessing uninitialized variables (when no values can reach an expression). E.g. in the following snippet value assigned to \fBfoo\fP on line 1 is unused, and variable \fBbar\fP is uninitialized on line 9: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C local foo = expr1() local bar if condition() then foo = expr2() bar = expr3() else foo = expr4() print(bar) end return foo, bar .ft P .fi .UNINDENT .UNINDENT .SS Secondary values and variables .sp Unused value assigned to a local variable is secondary if its origin is the last item on the RHS of assignment, and another value from that item is used. Secondary values typically appear when result of a function call is put into locals, and only some of them are later used. For example, here value assigned to \fBb\fP is secondary, value assigned to \fBc\fP is used, and value assigned to \fBa\fP is simply unused: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C local a, b, c = f(), g() return c .ft P .fi .UNINDENT .UNINDENT .sp A variable is secondary if all values assigned to it are secondary. In the snippet above, \fBb\fP is a secondary variable. .sp Warnings related to unused secondary values and variables can be removed using \fB\-s\fP/\fB\-\-no\-unused\-secondaries\fP CLI option or \fBunused_secondaries\fP config option. .SS Shadowing declarations (4xx) .sp Luacheck detects declarations of local variables shadowing previous declarations, unless the variable is named \fB_\fP\&. If the previous declaration is in the same scope as the new one, it is called redefining. .sp Note that it is \fBnot\fP necessary to define a new local variable when overwriting an argument: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C local function f(x) local x = x or "default" \-\- bad end local function f(x) x = x or "default" \-\- good end .ft P .fi .UNINDENT .UNINDENT .SS Control flow and data flow issues (5xx) .SS Unreachable code .sp Luacheck detects unreachable code. It also detects it if end of a loop block is unreachable, which means that the loop can be executed at most once: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C for i = 1, 100 do \-\- Break statement is outside the \(gaif\(ga block, \-\- so that the loop always stops after the first iteration. if cond(i) then f() end break end .ft P .fi .UNINDENT .UNINDENT .SS Unused labels .sp Labels that are not used by any \fBgoto\fP statements are reported as unused. .SS Unbalanced assignments .sp If an assignment has left side and right side with different lengths, the assignment is unbalanced and Luacheck warns about it. .sp An exception is initializing several local variables in a single statement while leaving some uninitialized: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C local a, b, c = nil \-\- Effectively sets \(gaa\(ga, \(gab\(ga, and \(gac\(ga to nil, no warning. .ft P .fi .UNINDENT .UNINDENT .SS Empty blocks .sp Luacheck warns about empty \fBdo\fP \fBend\fP blocks and empty \fBif\fP branches (\fBthen\fP \fBelse\fP, \fBthen\fP \fBelseif\fP, and \fBthen\fP \fBend\fP). .SS Empty statements .sp In Lua 5.2+ semicolons are considered statements and can appear even when not following normal statements. Such semicolons produce Luacheck warnings as they are completely useless. .SS Cyclomatic complexity .sp If a limit is set using \fB\-\-max\-cyclomatic\-complexity\fP CLI option or corresponding config or inline options, Luacheck warns about functions with too high cyclomatic complexity. .SS Reversed numeric for loops .sp Iterating a table in reverse using a numeric for loop going from \fB#t\fP to \fB1\fP requires a negative loop step. Luacheck warns about loops going from \fB#(some expression)\fP to \fB1\fP or a smaller constant when the loop step is not negative: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C \-\- Warning for this loop: \-\- numeric for loop goes from #(expr) down to 1 but loop step is not negative for i = #t, 1 do print(t[i]) end \-\- This loop is okay. for i = #t, 1, \-1 do print(t[i]) end .ft P .fi .UNINDENT .UNINDENT .SS Formatting issues (6xx) .SS Whitespace issues .sp Luacheck warns about trailing whitespace and inconsistent indentation (\fBSPACE\fP followed by \fBTAB\fP). .sp Some examples of trailing whitespace Luacheck finds: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C \-\- Whitespace example. print("Hello") print("World") .ft P .fi .UNINDENT .UNINDENT .sp Here: .INDENT 0.0 .IP \(bu 2 Any tabs or spaces after either \fB)\fP would be considered trailing. .IP \(bu 2 Any tabs or spaces after the \fB\&.\fP in the comment would be considered trailing .IP \(bu 2 Any tabs or spaces on the empty line between the two \fBprint\fP statements would also be considered a form of trailing whitespace. .UNINDENT .sp Trailing whitespace in any of these forms is useless, can be a nuisance to developers navigating around a file, and is forbidden in many formatting styles. .SS Line length limits .sp Luacheck warns about lines that are longer then some limit. Default limit is \fB120\fP characters. It\(aqs possible to change this limit using \fB\-\-max\-line\-length\fP CLI option or disable the check completely with \fB\-\-no\-max\-line\-length\fP; there are similar config and inline options. .sp Additionally, separate limits can be set for three different type of lines: .INDENT 0.0 .IP \(bu 2 "String" lines have their line ending inside a string, typically a long string using \fB[[...]]\fP syntax. .IP \(bu 2 "Comment" lines have their line ending inside a long comment (\fB\-\-[[...]]\fP), or end with a short comment using normal \fB\-\-...\fP syntax. .IP \(bu 2 "Code" lines are all other lines. .UNINDENT .sp These types of lines are limited using CLI options named \fB\-\-[no\-]max\-string\-line\-length\fP, \fB\-\-[no\-]max\-comment\-line\-length\fP, and \fB\-\-[no\-]max\-code\-line\-length\fP, with similar config and inline options. .SH COMMAND LINE INTERFACE .sp \fBluacheck\fP program accepts files, directories and \fI\%rockspecs\fP as arguments. They can be filtered using \fB\-\-include\-files\fP and \fB\-\-exclude\-files\fP options, see below. .INDENT 0.0 .IP \(bu 2 Given a file, \fBluacheck\fP will check it. .IP \(bu 2 Given \fB\-\fP, \fBluacheck\fP will check stdin. .IP \(bu 2 Given a directory, \fBluacheck\fP will check all files within it, selecting only files with \fB\&.lua\fP extension unless \fB\-\-include\-files\fP option is used. This feature requires \fI\%LuaFileSystem\fP (installed automatically if LuaRocks was used to install Luacheck). .IP \(bu 2 Given a rockspec (a file with \fB\&.rockspec\fP extension), \fBluacheck\fP will check all files with \fB\&.lua\fP extension mentioned in the rockspec in \fBbuild.install.lua\fP, \fBbuild.install.bin\fP and \fBbuild.modules\fP tables. .UNINDENT .sp The output of \fBluacheck\fP consists of separate reports for each checked file and ends with a summary: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C $ luacheck src Checking src/bad_code.lua 5 warnings src/bad_code.lua:3:16: unused variable helper 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 src/bad_code.lua:9:11: accessing undefined variable hepler Checking src/good_code.lua OK Checking src/python_code.lua 1 error src/python_code.lua:1:6: expected \(aq=\(aq near \(aq__future__\(aq Checking src/unused_code.lua 9 warnings src/unused_code.lua:3:18: unused argument baz src/unused_code.lua:4:8: unused loop variable i src/unused_code.lua:5:13: unused variable q src/unused_code.lua:7:11: unused loop variable a src/unused_code.lua:7:14: unused loop variable b src/unused_code.lua:7:17: unused loop variable c src/unused_code.lua:13:7: value assigned to variable x is unused src/unused_code.lua:14:1: value assigned to variable x is unused src/unused_code.lua:22:1: value assigned to variable z is unused Total: 14 warnings / 1 error in 4 files .ft P .fi .UNINDENT .UNINDENT .sp \fBluacheck\fP chooses exit code as follows: .INDENT 0.0 .IP \(bu 2 Exit code is \fB0\fP if no warnings or errors occurred. .IP \(bu 2 Exit code is \fB1\fP if some warnings occurred but there were no syntax errors or invalid inline options. .IP \(bu 2 Exit code is \fB2\fP if there were some syntax errors or invalid inline options. .IP \(bu 2 Exit code is \fB3\fP if some files couldn\(aqt be checked, typically due to an incorrect file name. .IP \(bu 2 Exit code is \fB4\fP if there was a critical error (invalid CLI arguments, config, or cache file). .UNINDENT .SS Command line options .sp Short options that do not take an argument can be combined into one, so that \fB\-qqu\fP is equivalent to \fB\-q \-q \-u\fP\&. For long options, both \fB\-\-option value\fP or \fB\-\-option=value\fP can be used. .sp Options taking several arguments can be used several times; \fB\-\-ignore foo \-\-ignore bar\fP is equivalent to \fB\-\-ignore foo bar\fP\&. .sp Note that options that may take several arguments, such as \fB\-\-globals\fP, should not be used immediately before positional arguments; given \fB\-\-globals foo bar file.lua\fP, \fBluacheck\fP will consider all \fBfoo\fP, \fBbar\fP and \fBfile.lua\fP global and then panic as there are no file names left. .TS center; |l|l|. _ T{ Option T} T{ Meaning T} _ T{ \fB\-g | \-\-no\-global\fP T} T{ Filter out warnings related to global variables. T} _ T{ \fB\-u | \-\-no\-unused\fP T} T{ Filter out warnings related to unused variables and values. T} _ T{ \fB\-r | \-\-no\-redefined\fP T} T{ Filter out warnings related to redefined variables. T} _ T{ \fB\-a | \-\-no\-unused\-args\fP T} T{ Filter out warnings related to unused arguments and loop variables. T} _ T{ \fB\-s | \-\-no\-unused\-secondaries\fP T} T{ Filter out warnings related to unused variables set together with used ones. .sp See secondaryvaluesandvariables T} _ T{ \fB\-\-no\-self\fP T} T{ Filter out warnings related to implicit \fBself\fP argument. T} _ T{ \fB\-\-std \fP T} T{ Set standard globals, default is \fBmax\fP\&. \fB\fP can be one of: .INDENT 0.0 .IP \(bu 2 \fBmax\fP \- union of globals of Lua 5.1, Lua 5.2, Lua 5.3 and LuaJIT 2.x; .IP \(bu 2 \fBmin\fP \- intersection of globals of Lua 5.1, Lua 5.2, Lua 5.3 and LuaJIT 2.x; .IP \(bu 2 \fBlua51\fP \- globals of Lua 5.1 without deprecated ones; .IP \(bu 2 \fBlua51c\fP \- globals of Lua 5.1; .IP \(bu 2 \fBlua52\fP \- globals of Lua 5.2; .IP \(bu 2 \fBlua52c\fP \- globals of Lua 5.2 compiled with LUA_COMPAT_ALL; .IP \(bu 2 \fBlua53\fP \- globals of Lua 5.3; .IP \(bu 2 \fBlua53c\fP \- globals of Lua 5.3 compiled with LUA_COMPAT_5_2; .IP \(bu 2 \fBluajit\fP \- globals of LuaJIT 2.x; .IP \(bu 2 \fBngx_lua\fP \- globals of Openresty \fI\%lua\-nginx\-module\fP 0.10.10, including standard LuaJIT 2.x globals; .IP \(bu 2 \fBlove\fP \- globals added by \fI\%LÖVE\fP; .IP \(bu 2 \fBbusted\fP \- globals added by Busted 2.0, by default added for files ending with \fB_spec.lua\fP within \fBspec\fP, \fBtest\fP, and \fBtests\fP subdirectories; .IP \(bu 2 \fBrockspec\fP \- globals allowed in rockspecs, by default added for files ending with \fB\&.rockspec\fP; .IP \(bu 2 \fBluacheckrc\fP \- globals allowed in Luacheck configs, by default added for files ending with \fB\&.luacheckrc\fP; .IP \(bu 2 \fBnone\fP \- no standard globals. .UNINDENT .sp See \fI\%Sets of standard globals\fP T} _ T{ \fB\-\-globals [] ...\fP T} T{ Add custom global variables or fields on top of standard ones. See \fI\%Defining extra globals and fields\fP T} _ T{ \fB\-\-read\-globals [] ...\fP T} T{ Add read\-only global variables or fields. T} _ T{ \fB\-\-new\-globals [] ...\fP T} T{ Set custom global variables or fields. Removes custom globals added previously. T} _ T{ \fB\-\-new\-read\-globals [] ...\fP T} T{ Set read\-only global variables or fields. Removes read\-only globals added previously. T} _ T{ \fB\-\-not\-globals [] ...\fP T} T{ Remove custom and standard global variables or fields. T} _ T{ \fB\-c | \-\-compat\fP T} T{ Equivalent to \fB\-\-std max\fP\&. T} _ T{ \fB\-d | \-\-allow\-defined\fP T} T{ Allow defining globals implicitly by setting them. .sp See implicitlydefinedglobals T} _ T{ \fB\-t | \-\-allow\-defined\-top\fP T} T{ Allow defining globals implicitly by setting them in the top level scope. .sp See implicitlydefinedglobals T} _ T{ \fB\-m | \-\-module\fP T} T{ Limit visibility of implicitly defined globals to their files. .sp See modules T} _ T{ \fB\-\-max\-line\-length \fP T} T{ Set maximum allowed line length (default: 120). T} _ T{ \fB\-\-no\-max\-line\-length\fP T} T{ Do not limit line length. T} _ T{ \fB\-\-max\-code\-line\-length \fP T} T{ Set maximum allowed length for lines ending with code (default: 120). T} _ T{ \fB\-\-no\-max\-code\-line\-length\fP T} T{ Do not limit code line length. T} _ T{ \fB\-\-max\-string\-line\-length \fP T} T{ Set maximum allowed length for lines within a string (default: 120). T} _ T{ \fB\-\-no\-max\-string\-line\-length\fP T} T{ Do not limit string line length. T} _ T{ \fB\-\-max\-comment\-line\-length \fP T} T{ Set maximum allowed length for comment lines (default: 120). T} _ T{ \fB\-\-no\-max\-comment\-line\-length\fP T} T{ Do not limit comment line length. T} _ T{ \fB\-\-max\-cyclomatic\-complexity \fP T} T{ Set maximum cyclomatic complexity for functions. T} _ T{ \fB\-\-no\-max\-cyclomatic\-complexity\fP T} T{ Do not limit function cyclomatic complexity (default). T} _ T{ \fB\-\-ignore | \-i [] ...\fP T} T{ Filter out warnings matching patterns. T} _ T{ \fB\-\-enable | \-e [] ...\fP T} T{ Do not filter out warnings matching patterns. T} _ T{ \fB\-\-only | \-o [] ...\fP T} T{ Filter out warnings not matching patterns. T} _ T{ \fB\-\-config \fP T} T{ Path to custom configuration file (default: \fB\&.luacheckrc\fP). T} _ T{ \fB\-\-no\-config\fP T} T{ Do not look up custom configuration file. T} _ T{ \fB\-\-default\-config \fP T} T{ Default path to custom configuration file, to be used if \fB\-\-[no\-]config\fP is not used and \fB\&.luacheckrc\fP is not found. .sp Default global location is: .INDENT 0.0 .IP \(bu 2 \fB%LOCALAPPDATA%\eLuacheck\e.luacheckrc\fP on Windows; .IP \(bu 2 \fB~/Library/Application Support/Luacheck/.luacheckrc\fP on OS X/macOS; .IP \(bu 2 \fB$XDG_CONFIG_HOME/luacheck/.luacheckrc\fP or \fB~/.config/luacheck/.luacheckrc\fP on other systems. .UNINDENT T} _ T{ \fB\-\-no\-default\-config\fP T} T{ Do not use fallback configuration file. T} _ T{ \fB\-\-filename \fP T} T{ Use another filename in output, for selecting configuration overrides and for file filtering. T} _ T{ \fB\-\-exclude\-files [] ...\fP T} T{ Do not check files matching these globbing patterns. Recursive globs such as \fB**/*.lua\fP are supported. T} _ T{ \fB\-\-include\-files [] ...\fP T} T{ Do not check files not matching these globbing patterns. T} _ T{ \fB\-\-cache []\fP T} T{ Path to cache file. (default: \fB\&.luacheckcache\fP). See \fI\%Caching\fP T} _ T{ \fB\-\-no\-cache\fP T} T{ Do not use cache. T} _ T{ \fB\-j | \-\-jobs\fP T} T{ Check \fB\fP files in parallel. Requires \fI\%LuaLanes\fP\&. Default number of jobs is set to number of available processing units. T} _ T{ \fB\-\-formatter \fP T} T{ Use custom formatter. \fB\fP must be a module name or one of: .INDENT 0.0 .IP \(bu 2 \fBTAP\fP \- Test Anything Protocol formatter; .IP \(bu 2 \fBJUnit\fP \- JUnit XML formatter; .IP \(bu 2 \fBvisual_studio\fP \- MSBuild/Visual Studio aware formatter; .IP \(bu 2 \fBplain\fP \- simple warning\-per\-line formatter; .IP \(bu 2 \fBdefault\fP \- standard formatter. .UNINDENT T} _ T{ \fB\-q | \-\-quiet\fP T} T{ Suppress report output for files without warnings. .INDENT 0.0 .IP \(bu 2 \fB\-qq\fP \- Suppress output of warnings. .IP \(bu 2 \fB\-qqq\fP \- Only output summary. .UNINDENT T} _ T{ \fB\-\-codes\fP T} T{ Show warning codes. T} _ T{ \fB\-\-ranges\fP T} T{ Show ranges of columns related to warnings. T} _ T{ \fB\-\-no\-color\fP T} T{ Do not colorize output. T} _ T{ \fB\-v | \-\-version\fP T} T{ Show version of Luacheck and its dependencies and exit. T} _ T{ \fB\-h | \-\-help\fP T} T{ Show help and exit. T} _ .TE .SS Patterns .sp CLI options \fB\-\-ignore\fP, \fB\-\-enable\fP and \fB\-\-only\fP and corresponding config options allow filtering warnings using pattern matching on warning codes, variable names or both. If a pattern contains a slash, the part before slash matches warning code and the part after matches variable name. Otherwise, if a pattern contains a letter or underscore, it matches variable name. Otherwise, it matches warning code. E.g.: .TS center; |l|l|. _ T{ Pattern T} T{ Matching warnings T} _ T{ 4.2 T} T{ Shadowing declarations of arguments or redefining them. T} _ T{ \&.*_ T} T{ Warnings related to variables with \fB_\fP suffix. T} _ T{ 4.2/.*_ T} T{ Shadowing declarations of arguments with \fB_\fP suffix or redefining them. T} _ .TE .sp Unless already anchored, patterns matching variable names are anchored at both sides and patterns matching warning codes are anchored at their beginnings. This allows one to filter warnings by category (e.g. \fB\-\-only 1\fP focuses \fBluacheck\fP on global\-related warnings). .SS Defining extra globals and fields .sp CLI options \fB\-\-globals\fP, \fB\-\-new\-globals\fP, \fB\-\-read\-globals\fP, \fB\-\-new\-read\-globals\fP, and corresponding config options add new allowed globals or fields. E.g. \fB\-\-read\-globals foo \-\-globals foo.bar\fP allows accessing \fBfoo\fP global and mutating its \fBbar\fP field. \fB\-\-not\-globals\fP also operates on globals and fields and removes definitions of both standard and custom globals. .SS Sets of standard globals .sp CLI option \fB\-\-stds\fP allows combining built\-in sets described above using \fB+\fP\&. For example, \fB\-\-std max\fP is equivalent to \fB\-\-std=lua51c+lua52c+lua53c+luajit\fP\&. Leading plus sign adds new sets to current one instead of replacing it. For instance, \fB\-\-std +love\fP is suitable for checking files using \fI\%LÖVE\fP framework. Custom sets of globals can be defined by mutating global variable \fBstds\fP in config. See custom_stds .SS Formatters .sp CLI option \fB\-\-formatter\fP allows selecting a custom formatter for \fBluacheck\fP output. A custom formatter is a Lua module returning a function with three arguments: report as returned by \fBluacheck\fP module (see report), array of file names and table of options. Options contain values assigned to \fBquiet\fP, \fBcolor\fP, \fBlimit\fP, \fBcodes\fP, \fBranges\fP and \fBformatter\fP options in CLI or config. Formatter function must return a string. .SS Caching .sp If LuaFileSystem is available, Luacheck can cache results of checking files. On subsequent checks, only files which have changed since the last check will be rechecked, improving run time significantly. Changing options (e.g. defining additional globals) does not invalidate cache. Caching can be enabled by using \fB\-\-cache \fP option or \fBcache\fP config option. Using \fB\-\-cache\fP without an argument or setting \fBcache\fP config option to \fBtrue\fP sets \fB\&.luacheckcache\fP as the cache file. Note that \fB\-\-cache\fP must be used every time \fBluacheck\fP is run, not on the first run only. .SS Stable interface for editor plugins and tools .sp Command\-line interface of Luacheck can change between minor releases. Starting from 0.11.0 version, the following interface is guaranteed at least till 1.0.0 version, and should be used by tools using Luacheck output, e.g. editor plugins. .INDENT 0.0 .IP \(bu 2 Luacheck should be started from the directory containing the checked file. .IP \(bu 2 File can be passed through stdin using \fB\-\fP as argument or using a temporary file. Real filename should be passed using \fB\-\-filename\fP option. .IP \(bu 2 Plain formatter should be used. It outputs one issue (warning or error) per line. .IP \(bu 2 To get precise error location, \fB\-\-ranges\fP option can be used. Each line starts with real filename (passed using \fB\-\-filename\fP), followed by \fB::\-:\fP, where \fB\fP is line number on which issue occurred and \fB\-\fP is inclusive range of columns of token related to issue. Numbering starts from 1. If \fB\-\-ranges\fP is not used, end column and dash is not printed. .IP \(bu 2 To get warning and error codes, \fB\-\-codes\fP option can be used. For each line, substring between parentheses contains three digit issue code, prefixed with \fBE\fP for errors and \fBW\fP for warnings. Lack of such substring indicates a fatal error (e.g. I/O error). .IP \(bu 2 The rest of the line is warning message. .UNINDENT .sp If compatibility with older Luacheck version is desired, output of \fBluacheck \-\-help\fP can be used to get its version. If it contains string \fB0..\fP, where \fB\fP is at least 11 and \fBpatch\fP is any number, interface described above should be used. .SH CONFIGURATION FILE .sp \fBluacheck\fP tries to load configuration from \fB\&.luacheckrc\fP file in the current directory. If not found, it will look for it in the parent directory and so on, going up until it reaches file system root. Path to config can be set using \fB\-\-config\fP option, in which case it will be used during recursive loading. Paths within config are interpreted relatively to the directory from which it was loaded. .sp Config loading can be disabled using \fB\-\-no\-config\fP flag. .sp If neither of \fB\-\-config\fP, \fB\-\-no\-config\fP, and \fB\-\-no\-default\-config\fP options are used, \fBluacheck\fP will attempt to load configuration from value of \fB\-\-default\-config\fP option, or \fB%LOCALAPPDATA%\eLuacheck\e.luacheckrc\fP on Windows, \fB~/Library/Application Support/Luacheck/.luacheckrc\fP on OS X/macOS, and \fB$XDG_CONFIG_HOME/luacheck/.luacheckrc\fP or \fB~/.config/luacheck/.luacheckrc\fP on other systems by default. Paths within default config are interpreted relatively to the current directory. .sp Config is simply a Lua script executed by \fBluacheck\fP\&. It may set various options by assigning to globals or by returning a table with option names as keys. .sp Options loaded from config have the lowest priority: it\(aqs possible to overwrite them with CLI options or inline options. .SS Config options .TS center; |l|l|l|. _ T{ Option T} T{ Type T} T{ Default value T} _ T{ \fBquiet\fP T} T{ Integer in range 0..3 T} T{ \fB0\fP T} _ T{ \fBcolor\fP T} T{ Boolean T} T{ \fBtrue\fP T} _ T{ \fBcodes\fP T} T{ Boolean T} T{ \fBfalse\fP T} _ T{ \fBranges\fP T} T{ Boolean T} T{ \fBfalse\fP T} _ T{ \fBformatter\fP T} T{ String or function T} T{ \fB"default"\fP T} _ T{ \fBcache\fP T} T{ Boolean or string T} T{ \fBfalse\fP T} _ T{ \fBjobs\fP T} T{ Positive integer T} T{ \fB1\fP T} _ T{ \fBexclude_files\fP T} T{ Array of strings T} T{ \fB{}\fP T} _ T{ \fBinclude_files\fP T} T{ Array of strings T} T{ (Include all files) T} _ T{ \fBglobal\fP T} T{ Boolean T} T{ \fBtrue\fP T} _ T{ \fBunused\fP T} T{ Boolean T} T{ \fBtrue\fP T} _ T{ \fBredefined\fP T} T{ Boolean T} T{ \fBtrue\fP T} _ T{ \fBunused_args\fP T} T{ Boolean T} T{ \fBtrue\fP T} _ T{ \fBunused_secondaries\fP T} T{ Boolean T} T{ \fBtrue\fP T} _ T{ \fBself\fP T} T{ Boolean T} T{ \fBtrue\fP T} _ T{ \fBstd\fP T} T{ String or set of standard globals T} T{ \fB"max"\fP T} _ T{ \fBglobals\fP T} T{ Array of strings or field definition map T} T{ \fB{}\fP T} _ T{ \fBnew_globals\fP T} T{ Array of strings or field definition map T} T{ (Do not overwrite) T} _ T{ \fBread_globals\fP T} T{ Array of strings or field definition map T} T{ \fB{}\fP T} _ T{ \fBnew_read_globals\fP T} T{ Array of strings or field definition map T} T{ (Do not overwrite) T} _ T{ \fBnot_globals\fP T} T{ Array of strings T} T{ \fB{}\fP T} _ T{ \fBcompat\fP T} T{ Boolean T} T{ \fBfalse\fP T} _ T{ \fBallow_defined\fP T} T{ Boolean T} T{ \fBfalse\fP T} _ T{ \fBallow_defined_top\fP T} T{ Boolean T} T{ \fBfalse\fP T} _ T{ \fBmodule\fP T} T{ Boolean T} T{ \fBfalse\fP T} _ T{ \fBmax_line_length\fP T} T{ Number or \fBfalse\fP T} T{ \fB120\fP T} _ T{ \fBmax_code_line_length\fP T} T{ Number or \fBfalse\fP T} T{ \fB120\fP T} _ T{ \fBmax_string_line_length\fP T} T{ Number or \fBfalse\fP T} T{ \fB120\fP T} _ T{ \fBmax_comment_line_length\fP T} T{ Number or \fBfalse\fP T} T{ \fB120\fP T} _ T{ \fBmax_cyclomatic_complexity\fP T} T{ Number or \fBfalse\fP T} T{ \fBfalse\fP T} _ T{ \fBignore\fP T} T{ Array of patterns (see patterns) T} T{ \fB{}\fP T} _ T{ \fBenable\fP T} T{ Array of patterns T} T{ \fB{}\fP T} _ T{ \fBonly\fP T} T{ Array of patterns T} T{ (Do not filter) T} _ .TE .sp An example of a config which makes \fBluacheck\fP ensure that only globals from the portable intersection of Lua 5.1, Lua 5.2, Lua 5.3 and LuaJIT 2.0 are used, as well as disables detection of unused arguments: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C std = "min" ignore = {"212"} .ft P .fi .UNINDENT .UNINDENT .SS Custom sets of globals .sp \fBstd\fP option allows setting a custom standard set of globals using a table. This table can have two fields: \fBglobals\fP and \fBread_globals\fP\&. Both of them should contain a field definition map defining some globals. The simplest way to define globals is to list their names: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C std = { globals = {"foo", "bar"}, \-\- these globals can be set and accessed. read_globals = {"baz", "quux"} \-\- these globals can only be accessed. } .ft P .fi .UNINDENT .UNINDENT .sp For globals defined like this Luacheck will additionally consider any fields within them defined. To define a global with a restricted set of fields, use global name as key and a table as value. In that table, \fBfields\fP subtable can contain the fields in the same format: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C std = { read_globals = { foo = { \-\- Defining read\-only global \(gafoo\(ga... fields = { field1 = { \-\- \(gafoo.field1\(ga is now defined... fields = { nested_field = {} \-\- \(gafoo.field1.nested_field\(ga is now defined... } }, field2 = {} \-\- \(gafoo.field2\(ga is defined. } } } } .ft P .fi .UNINDENT .UNINDENT .sp Globals and fields can be marked read\-only or not using \fBread_only\fP property with a boolean value. Property \fBother_fields\fP controls whether the global or field can also contain other unspecified fields: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C std = { read_globals = { foo = { \-\- \(gafoo\(ga and its fields are read\-only by default (because they are within \(garead_globals\(ga table). fields = { bar = { read_only = false, \-\- \(gafoo.bar\(ga is not read\-only, can be set. other_fields = true, \-\- \(gafoo.bar[anything]\(ga is defined and can be set or mutated (inherited from \(gafoo.bar\(ga). fields = { baz = {read_only = true}, \-\- \(gafoo.bar.baz\(ga is read\-only as an exception. } } } } } } .ft P .fi .UNINDENT .UNINDENT .sp Custom sets can be given names by mutating global \fBstds\fP variable, so that they can then be used in \fB\-\-std\fP CLI option and \fBstd\fP inline and config option. .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C stds.some_lib = {...} std = "min+some_lib" .ft P .fi .UNINDENT .UNINDENT .sp In config, \fBglobals\fP, \fBnew_globals\fP, \fBread_globals\fP, and \fBnew_read_globals\fP can also contain definitions in same format: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C read_globals = { server = { fields = { \-\- Allow mutating \(gaserver.sessions\(ga with any keys... sessions = {read_only = false, other_fields = true}, \-\- other fields... } }, \-\-\- other globals... } .ft P .fi .UNINDENT .UNINDENT .SS Per\-file and per\-path overrides .sp The environment in which \fBluacheck\fP loads the config contains a special global \fBfiles\fP\&. When checking a file \fB\fP, \fBluacheck\fP will override options from the main config with entries from \fBfiles[]\fP if \fB\fP matches \fB\fP, applying entries for more general globs first. For example, the following config re\-enables detection of unused arguments only for files in \fBsrc/dir\fP, but not for files ending with \fB_special.lua\fP: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C std = "min" ignore = {"212"} files["src/dir"] = {enable = {"212"}} files["src/dir/**/*_special.lua"] = {ignore = {"212"}} .ft P .fi .UNINDENT .UNINDENT .sp Note that \fBfiles\fP table supports autovivification, so that .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C files["src/dir"].enable = {"212"} .ft P .fi .UNINDENT .UNINDENT .sp and .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C files["src/dir"] = {enable = {"212"}} .ft P .fi .UNINDENT .UNINDENT .sp are equivalent. .SS Default per\-path std overrides .sp \fBluacheck\fP uses a set of default per\-path overrides: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C files["**/spec/**/*_spec.lua"].std = "+busted" files["**/test/**/*_spec.lua"].std = "+busted" files["**/tests/**/*_spec.lua"].std = "+busted" files["**/*.rockspec"].std = "+rockspec" files["**/*.luacheckrc"].std = "+luacheckrc" .ft P .fi .UNINDENT .UNINDENT .sp Each of these can be overriden by setting a different \fBstd\fP value for the corresponding key in \fBfiles\fP\&. .SH INLINE OPTIONS .sp Luacheck supports setting some options directly in the checked files using inline configuration comments. These inline options have the highest priority, overwriting both config options and CLI options. .sp An inline configuration comment is a short comment starting with \fBluacheck:\fP label, possibly after some whitespace. The body of the comment should contain comma separated options, where option invocation consists of its name plus space separated arguments. It can also contain notes enclosed in balanced parentheses, which are ignored. The following options are supported: .TS center; |l|l|. _ T{ Option T} T{ Number of arguments T} _ T{ global T} T{ 0 T} _ T{ unused T} T{ 0 T} _ T{ redefined T} T{ 0 T} _ T{ unused args T} T{ 0 T} _ T{ unused secondaries T} T{ 0 T} _ T{ self T} T{ 0 T} _ T{ compat T} T{ 0 T} _ T{ module T} T{ 0 T} _ T{ allow defined T} T{ 0 T} _ T{ allow defined top T} T{ 0 T} _ T{ max line length T} T{ 1 (with \fBno\fP and no arguments disables line length checks) T} _ T{ max code line length T} T{ 1 (with \fBno\fP and no arguments disables code line length checks) T} _ T{ max string line length T} T{ 1 (with \fBno\fP and no arguments disables string line length checks) T} _ T{ max comment line length T} T{ 1 (with \fBno\fP and no arguments disables comment line length checks) T} _ T{ std T} T{ 1 T} _ T{ globals T} T{ 0+ T} _ T{ new globals T} T{ 0+ T} _ T{ read globals T} T{ 0+ T} _ T{ new read globals T} T{ 0+ T} _ T{ not globals T} T{ 0+ T} _ T{ ignore T} T{ 0+ (without arguments everything is ignored) T} _ T{ enable T} T{ 1+ T} _ T{ only T} T{ 1+ T} _ .TE .sp Options that take no arguments can be prefixed with \fBno\fP to invert their meaning. E.g. \fB\-\-luacheck: no unused args\fP disables unused argument warnings. .sp Part of the file affected by inline option dependes on where it is placed. If there is any code on the line with the option, only that line is affected; otherwise, everything till the end of the current closure is. In particular, inline options at the top of the file affect all of it: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C \-\- luacheck: globals g1 g2, ignore foo local foo = g1(g2) \-\- No warnings emitted. \-\- The following unused function is not reported. local function f() \-\- luacheck: ignore \-\- luacheck: globals g3 g3() \-\- No warning. end g3() \-\- Warning is emitted as the inline option defining g3 only affected function f. .ft P .fi .UNINDENT .UNINDENT .sp For fine\-grained control over inline option visibility use \fBluacheck: push\fP and \fBluacheck: pop\fP directives: .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C \-\- luacheck: push ignore foo foo() \-\- No warning. \-\- luacheck: pop foo() \-\- Warning is emitted. .ft P .fi .UNINDENT .UNINDENT .SH LUACHECK MODULE .sp Use \fBlocal luacheck = require "luacheck"\fP to import \fBluacheck\fP module. It contains the following functions: .INDENT 0.0 .IP \(bu 2 \fBluacheck.get_report(source)\fP: Given source string, returns analysis data (a table). .IP \(bu 2 \fBluacheck.process_reports(reports, options)\fP: Processes array of analysis reports and applies options. \fBreports[i]\fP uses \fBoptions\fP, \fBoptions[i]\fP, \fBoptions[i][1]\fP, \fBoptions[i][2]\fP, ... as options, overriding each other in that order. Options table is a table with fields similar to config options; see options\&. Analysis reports with field \fBfatal\fP are ignored. \fBprocess_reports\fP returns final report, see \fI\%Report format\fP\&. .IP \(bu 2 \fBluacheck.check_strings(sources, options)\fP: Checks array of sources using options, returns final report. Tables with field \fBfatal\fP within \fBsources\fP array are ignored. .IP \(bu 2 \fBluacheck.check_files(files, options)\fP: Checks array of files using options, returns final report. Open file handles can passed instead of filenames, in which case they will be read till EOF and closed. .IP \(bu 2 \fBluacheck.get_message(issue)\fP: Returns a string message for an issue, see \fI\%Report format\fP\&. .UNINDENT .sp \fBluacheck._VERSION\fP contains Luacheck version as a string in \fBMAJOR.MINOR.PATCH\fP format. .sp Using \fBluacheck\fP as a function is equivalent to calling \fBluacheck.check_files\fP\&. .SS Report format .sp A final report is an array of file reports plus fields \fBwarnings\fP, \fBerrors\fP and \fBfatals\fP containing total number of warnings, errors and fatal errors, correspondingly. .sp A file report is an array of issues (warnings or errors). If a fatal error occurred while checking a file, its report will have \fBfatal\fP field containing error type and \fBmsg\fP field containing error message. .sp An issue is a table with field \fBcode\fP indicating its type (see warnings), and fields \fBline\fP, \fBcolumn\fP and \fBend_column\fP pointing to the source of the warning. \fBname\fP field may contain name of related variable. Issues of some types can also have additional fields: .TS center; |l|l|. _ T{ Codes T} T{ Additional fields T} _ T{ 011 T} T{ \fBmsg\fP field contains syntax error message. T} _ T{ 111 T} T{ \fBmodule\fP field indicates that assignment is to a non\-module global variable. T} _ T{ 122, 142, 143 T} T{ \fBindirect\fP field indicates that the global field was accessed using a local alias. T} _ T{ 122, 142, 143 T} T{ \fBfield\fP field contains string representation of related global field. T} _ T{ 211 T} T{ \fBfunc\fP field indicates that unused variable is a function. T} _ T{ 211 T} T{ \fBrecursive\fP field indicates that unused function is recursive. T} _ T{ 211 T} T{ \fBmutually_recursive\fP field is set for unused mutually recursive functions. T} _ T{ 314 T} T{ \fBfield\fP field contains string representation of ununsed field or index. T} _ T{ 011 T} T{ \fBprev_line\fP, \fBprev_column\fP, and \fBprev_end_column\fP fields may point to an extra relevant location, such as the opening unpaired bracket. T} _ T{ 4.. T} T{ \fBprev_line\fP, \fBprev_column\fP, and \fBprev_end_column\fP fields contain location of the overwritten definition. T} _ T{ 521 T} T{ \fBlabel\fP field contains label name. T} _ T{ 631 T} T{ \fBline_ending\fP field contains \fB"comment"\fP or \fB"string"\fP if line ending is within a comment or a string. T} _ T{ 631 T} T{ \fBmax_length\fP field contains maximum allowed line length. T} _ .TE .sp Other fields may be present for internal reasons. .sp This is documentation for 0.24.0 version of \fI\%Luacheck\fP, a linter for \fI\%Lua\fP\&. .SH AUTHOR Peter Melnichenko .SH COPYRIGHT 2020 - 2018, Peter Melnichenko .\" Generated by docutils manpage writer. .