Description
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
returnsfalse
. - After formatting,
exampleFunction2
returnsnothing
instead offalse
.
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.