Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

always_use_return and short_circuit_to_if` bug #887

Open
edward-bestx opened this issue Nov 24, 2024 · 0 comments
Open

always_use_return and short_circuit_to_if` bug #887

edward-bestx opened this issue Nov 24, 2024 · 0 comments

Comments

@edward-bestx
Copy link

I think I have found a bug which I think is related to the combination of two settings:

always_use_return = true
short_circuit_to_if = true

Here is some example code which I subsequently ran the formatter on.

cat ExampleModule2.jl
module ExampleModule2

function __init__()
    global LOGFILEHANDLE = "notset"
    return nothing
end

function write(logfilehandle, something)
    println("writing $(something) to logfilehandle=$(logfilehandle)")
    return 10
end

function exampleFunction()
    LOGFILEHANDLE != "notset" && write(LOGFILEHANDLE, "examplestring")
end

function whatDoesExampleFunctionReturn()
    tmp = exampleFunction()
    println("type: $(typeof(tmp))")
    println(tmp)
end

end

And here is the result of the format. I change the function names so that you can combine both of these into a single Julia file, if you wish to do so.

cat ExampleModule2.jl
module ExampleModule2

function __init__()
    global LOGFILEHANDLE = "notset"
    return nothing
end

function write(logfilehandle, something)
    println("writing $(something) to logfilehandle=$(logfilehandle)")
    return 10
end

function exampleFunction2()
    return if LOGFILEHANDLE != "notset"
        write(LOGFILEHANDLE, "examplestring")
    end
end

function whatDoesExampleFunction2Return()
    tmp = exampleFunction2()
    println("type: $(typeof(tmp))")
    println(tmp)
end

end

Here's the result of running both, combined into a single file ExampleModule2.jl:

julia> using ExampleModule2
[ Info: Precompiling ExampleModule2 [top-level] (cache misses: include_dependency fsize change (2))

julia> ExampleModule2.whatDoesExampleFunctionReturn()
type: Bool
false

julia> ExampleModule2.whatDoesExampleFunction2Return()
type: Nothing
nothing

julia> ExampleModule2.LOGFILEHANDLE = "set"
"set"

julia> ExampleModule2.whatDoesExampleFunctionReturn()
writing examplestring to logfilehandle=set
type: Int64
10

julia> ExampleModule2.whatDoesExampleFunction2Return()
writing examplestring to logfilehandle=set
type: Int64
10

What is important to note is that the formatter changes the behavior of exampleFunction.

  • When LOGFILEHANDLE = "notset", exampleFunction returns false.
  • After formatting, exampleFunction2 returns nothing instead of false.

Hopefully that is clear?

Maybe the formatter should do this instead?

function exampleFunction2()
    return (
        if LOGFILEHANDLE != "notset"
            write(LOGFILEHANDLE, "examplestring")
        else
            false
        end
    )
end

It's actually kind of hard to say what it should do with

LOGFILEHANDLE != "notset" && write(LOGFILEHANDLE, "examplestring")

because this isn't really a very sensible piece of code. But it does return false if the first condition is false and then it returns the return value of write otherwise.

@edward-bestx edward-bestx changed the title always_use_return and short_circuit_to_if`` bug always_use_return and short_circuit_to_if` bug Nov 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant