seaofvoices / darklua

A command line tool that transforms Lua code
https://darklua.com/
MIT License
71 stars 9 forks source link

`remove_assertions` changes return value when used in a call expression #189

Open sircfenner opened 3 months ago

sircfenner commented 3 months ago

For example, applying remove_assertions with the default parameter for the rule, the following input:

local head = assert(character:FindFirstChild("Head"), "head is missing!")

becomes:

local head = character:FindFirstChild("Head") and nil

This changes runtime behavior in an unexpected way, because head is always nil even when the character does have a child named Head. Expected output is:

local head = character:FindFirstChild("Head")

When preserve_aguments_side_effects is set to false, the output is:

local head = nil

Not sure if this is intended either. I think whether the return value is used (so, whether the assert is an expression or a statement?) should probably be factored in here.

Tested on version 0.13.0.

jeparlefrancais commented 3 months ago

Oh interesting! I believed I might have taken for granted that assert returned nothing, as I've used it mostly just like a call statement like:

assert(condition, "message")

Thank you for reporting the issue! It does not seem complicated so it should be fixed soon 👍