darklua
DocumentationTry itGitHub

remove_unused_if_branch


Added in 0.3.1

When a condition in a if branch (if condition then or elseif condition then) can be evaluated to a known value, the if statement (or if expression) is modified to remove branches that become useless.

if unknown then
    return 2
elseif true then
    return 1
else
    return 0
end

Since the second branch is always true, the else block becomes superfluous. As such, this rule would output:

if unknown then
    return 2
elseif true then
    return 1
end

This rule can also turn if statements into do statements in certain cases:

if true then
    return 2
elseif unknown then
    return 1
else
    return 0
end

Since the first branch is always true, all other branches are useless, so this rule would output:

do
    return 2
end

This rule is influenced by the evaluation system of darklua. The more darklua can evaluate code, the better this rule can be applied.

Examples


return if true then value else default
return if false then value else default
if false then
    local sum = 0
    for _, element in ipairs(array) do
        sum = sum + element
    end
    print("sum of array:", sum)
end
InputOutput
return if true then value else default
return value 
return if false then value else default
return default
if false then
    local sum = 0
    for _, element in ipairs(array) do
        sum = sum + element
    end
    print("sum of array:", sum)
end