dalexeev / gdscript-preprocessor

An export plugin for stripping comments and "conditional compilation" of GDScript.
MIT License
78 stars 1 forks source link

Simplifying if statements inside functions that are supposed to return a value causes not all pathes to return a value. #5

Closed Pshy0 closed 5 months ago

Pshy0 commented 5 months ago

This is not valid GDScript: image

Once run, it will result in errors: image

Pshy0 commented 5 months ago

One solution could be to get rid of the "if true" completely. If the block define variables with the same name than the block above, this is already supposed to generate a warning.

dalexeev commented 5 months ago

One solution could be to get rid of the "if true" completely.

This is how this plugin was originally implemented. Getting rid of if true is not possible, since variables can be declared inside the block, the following code will stop working:

func test():
    if OS.is_debug_build():
        var a = 1
        print(a)
    var a = 2
    print(a)

A possible solution is to analyze returns and keep the else branch with a fake return even if the else branch should be removed.

dalexeev commented 5 months ago

Fixed in 461016c29c418713028e797822ab04b83d51fa50. Thanks for testing! Please let me know if you find any new issues.

Currently, only the current block is analyzed, a fake return is always inserted at the end of function (if it did not already exist). This may be improved in the future.