Remove constant function calls
Takes something like this:
local function getFoo()
return "foo"
end
local var = getFoo()
And turns into:
local function getFoo()
return "foo"
end
local var = "foo"
The function itself does not have to be cleaned up by this rule, it can be left to a rule that removes unused variables (#26)
In GitLab by @matthargett on Sep 20, 2021, 14:16
a wrinkle:
local function getFoo()
return "foo"
end
local function g(callback)
return callback()
end
local function main()
print(g(getFoo))
end
ideally, we could do const propagation such that the resulting code was
local function main()
print("foo")
end
but that's a tall order analysis-wise. at the very least, this kind of code needs to not transform in a way that breaks functionality or makes the code larger.
In GitLab by @matthargett on Jan 2, 2022, 22:28
An example where this would be most useful in React 17 would be with the resolveDispatcher() function in ReactCurrentDispatcher. Everywhere that resolveDispatcher() is called should inlined to be ReactCurrentDispatcher.current (when __DEV__=false).
It's a simple reassignment before returning in that function, and virtual execution should be able to track that, the inline rule inlines, and the remove unused variable eliminates the then fully-inlined resolveDispatcher().
As suggested in https://github.com/seaofvoices/darklua/issues/191, the rule should be able to inline empty functions
Should this work for pcall() aswell? I can't give an example why you would pcall a function that's always expected to return the same thing, but it would still be nice to have.
Example:
local function getFoo()
return "foo"
end
local success, result = pcall(getFoo)
into
local function getFoo()
return "foo"
end
local success, result = true, "foo"