Skip to content

Commit 1da6b98

Browse files
committed
fix some issues with removing unused code and refactor tests
1 parent 1460a15 commit 1da6b98

4 files changed

Lines changed: 178 additions & 137 deletions

File tree

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
},
1313
"editor.formatOnSave": true,
1414
"editor.defaultFormatter": "unpublished.nattlua",
15-
15+
"nattlua.removeUnusedOnSave": false,
1616
"nattlua.executable": "luajit",
1717
"nattlua.workingDirectory": "${workspaceFolder}",
1818
"nattlua.arguments": ["nattlua.lua", "lsp"],

nattlua/analyzer/statements/if.lua

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,19 @@ return {
2424
}
2525
)
2626
self:ClearTracked()
27+
elseif self.config.remove_unused and obj:IsFalsy() then
28+
table_insert(
29+
blocks,
30+
{
31+
statements = statements,
32+
tracked_objects = self:GetTrackedObjects(),
33+
obj = obj,
34+
}
35+
)
2736
end
2837

2938
if self:IsRuntime() then
30-
if obj:IsCertainlyFalse() then
39+
if obj:IsCertainlyFalse() or obj:IsUncertain() then
3140
self:ConstantIfExpressionWarning(error_messages.if_always_false())
3241

3342
for _, statement in ipairs(statements) do
@@ -137,4 +146,4 @@ return {
137146

138147
self:ClearTracked()
139148
end,
140-
}
149+
}

nattlua/emitter/base.lua

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,8 @@ return function()
101101
if all_unused then
102102
if node.right then
103103
for _, expr in ipairs(node.right) do
104-
if
105-
expr.Type ~= "expression_value" and
106-
not (
104+
local is_safe = expr.Type == "expression_value" or
105+
(
107106
expr.Type == "expression_postfix_call" and
108107
(
109108
expr.import_expression or
@@ -113,14 +112,19 @@ return function()
113112
)
114113
)
115114
)
116-
then
117-
return false
115+
116+
if not is_safe and expr.Type == "expression_binary_operator" then
117+
is_safe = true
118118
end
119+
120+
if not is_safe then return false end
119121
end
120122
end
121123

122124
return true
123125
end
126+
127+
return false
124128
elseif
125129
node.Type == "statement_local_function" or
126130
node.Type == "statement_local_analyzer_function" or
@@ -129,7 +133,10 @@ return function()
129133
return self:IsNodeUnused(node.tokens["identifier"])
130134
elseif node.Type == "statement_local_type" then
131135
return self:IsNodeUnused(node.left)
132-
elseif node.Type == "statement_local_declaration" or node.Type == "statement_local_analyzer_declaration" then
136+
elseif
137+
node.Type == "statement_local_declaration" or
138+
node.Type == "statement_local_analyzer_declaration"
139+
then
133140
local all_unused = true
134141

135142
for _, left in ipairs(node.left) do
@@ -2325,4 +2332,4 @@ return function()
23252332
end
23262333

23272334
return META
2328-
end
2335+
end

0 commit comments

Comments
 (0)