Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore Luacheck warnings?

Tags:

lua

luacheck

The Luacheck linter produces a warning when an if statement branch doesn't include any statements. For example, if I have a file called test.lua with the following code

local function f(x)
    if x == "hello" then
        -- nothing to do
    elseif x == "world" then
        print(17)
    else
        error("bad value for x")
    end
end

f("world")

Then running luacheck test.lua will produce the following diagnostic

Checking test.lua                                 1 warning

    test.lua:2:21: empty if branch

Total: 1 warning / 0 errors in 1 file

Is there a way to work around this warning? As far as I know there is no configuration option to disable it and trying to some empty statements with a semicolon would not silence the warning either (in fact it would just add an additional warning about the empty statement):

if x == "hello" then
    ;
elseif ...

At the moment the only workaround I can think of involves creating an extra layer of if statements, which I think is less clear than the original version.

if x ~= "hello" then
    if x == "world" then
        print(17)
    else
        error("impossible")
    end
end
like image 804
hugomg Avatar asked Mar 07 '18 17:03

hugomg


3 Answers

luacheck test.lua --ignore 542

Please refer to the Luacheck documentation. Command Line Interface

CLI options --ignore, --enable and --only and corresponding config options allow filtering warnings using pattern matching on warning codes,...

List of Warnings

Code | Description

542 | An empty if branch.

Alternatively, it is also possible to disable a warning by setting the ignore option in a .luacheckrc Configuration File:

ignore = {"542"}

Personally I don't like ignoring warnings. I prefer to fix their cause.

So my solution to your particular problem would be to simply rearrange the conditionals. This does not require an extra if layer as in the alternative you came up with.

local function f(x)
    if x == "world" then
        print(17)
    elseif x ~= "hello" then
        error("bad value for x")
    end
end

f("world")
like image 89
Piglet Avatar answered Nov 04 '22 14:11

Piglet


The CLI and .luacheckrc options are good, but I also find inline comments to be most useful.

This page:Luacheck Inline Commandsl shows many of the options. For instance: -- luacheck:ignore 111 will disable the non-standard global warning. Another fun gem: push and pop

-- luacheck: push ignore foo
foo() -- No warning.
-- luacheck: pop
foo() -- Warning is emitted.

This lets you disable a rule, or a different lint function, and then quickly undo it.

like image 45
TonyH Avatar answered Nov 04 '22 14:11

TonyH


The best way would be to fix the warning, but if that is not possile, you can ignore them.

I found no way of ignoring all warnings with one flag, but you can just list them all and ignore them with this in your config file:

ignore = {
    "011", -- A syntax error.
    "021", -- An invalid inline option.
    "022", -- An unpaired inline push directive.
    "023", -- An unpaired inline pop directive.
    "111", -- Setting an undefined global variable.
    "112", -- Mutating an undefined global variable.
    "113", -- Accessing an undefined global variable.
    "121", -- Setting a read-only global variable.
    "122", -- Setting a read-only field of a global variable.
    "131", -- Unused implicitly defined global variable.
    "142", -- Setting an undefined field of a global variable.
    "143", -- Accessing an undefined field of a global variable.
    "211", -- Unused local variable.
    "212", -- Unused argument.
    "213", -- Unused loop variable.
    "221", -- Local variable is accessed but never set.
    "231", -- Local variable is set but never accessed.
    "232", -- An argument is set but never accessed.
    "233", -- Loop variable is set but never accessed.
    "241", -- Local variable is mutated but never accessed.
    "311", -- Value assigned to a local variable is unused.
    "312", -- Value of an argument is unused.
    "313", -- Value of a loop variable is unused.
    "314", -- Value of a field in a table literal is unused.
    "321", -- Accessing uninitialized local variable.
    "331", -- Value assigned to a local variable is mutated but never accessed.
    "341", -- Mutating uninitialized local variable.
    "411", -- Redefining a local variable.
    "412", -- Redefining an argument.
    "413", -- Redefining a loop variable.
    "421", -- Shadowing a local variable.
    "422", -- Shadowing an argument.
    "423", -- Shadowing a loop variable.
    "431", -- Shadowing an upvalue.
    "432", -- Shadowing an upvalue argument.
    "433", -- Shadowing an upvalue loop variable.
    "511", -- Unreachable code.
    "512", -- Loop can be executed at most once.
    "521", -- Unused label.
    "531", -- Left-hand side of an assignment is too short.
    "532", -- Left-hand side of an assignment is too long.
    "541", -- An empty do end block.
    "542", -- An empty if branch.
    "551", -- An empty statement.
    "561", -- Cyclomatic complexity of a function is too high.
    "571", -- A numeric for loop goes from #(expr) down to 1 or less without negative step.
    "611", -- A line consists of nothing but whitespace.
    "612", -- A line contains trailing whitespace.
    "613", -- Trailing whitespace in a string.
    "614", -- Trailing whitespace in a comment.
    "621", -- Inconsistent indentation (SPACE followed by TAB).
    "631", -- Line is too long.
}

Ignoring an error is more difficult, which is a problem, since there are some false errors like: https://github.com/mpeterv/luacheck/issues/152

like image 42
rubo77 Avatar answered Nov 04 '22 14:11

rubo77