darklua icon indicating copy to clipboard operation
darklua copied to clipboard

Remove constant function calls

Open jeparlefrancais opened this issue 4 years ago • 4 comments

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)

jeparlefrancais avatar Sep 01 '21 14:09 jeparlefrancais

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.

jeparlefrancais avatar Sep 20 '21 18:09 jeparlefrancais

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().

jeparlefrancais avatar Jan 03 '22 03:01 jeparlefrancais

As suggested in https://github.com/seaofvoices/darklua/issues/191, the rule should be able to inline empty functions

jeparlefrancais avatar May 06 '24 21:05 jeparlefrancais

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"

ewd3v avatar Nov 07 '24 11:11 ewd3v