Is there a way to ask the compiler to issue a warning, whenever a proc does not have a return statement in all branches?
This came up when I am writing a toy compiler, and in a long proc that has many conditional branches, I missed some return statements in some branches. Ofc, I can go back/inspect/fix, but without compiler assistance, I don't feel completely confident that I covered all the branches and would have to rely on a lot of unit tests.
To make this more concrete, this is a toy example of the sort of code I am talking about:
type
X = ref Xobj
Xobj = object
x: int
Ret = ref RetObj
RetObj = object
y: int
proc fn(x: X): Ret =
if isNil(x): # just an example, any condition is fine
echo "ok"
else:
echo "ok too"
return Ret(y:42)
var x: X
doAssert isNil(fn(x))
I thought this might trigger the ProveInit warning, which I can do --warningAsError:ProveInit:on to detect, but it did not trigger.
I looked at the strictNotNil experimental feature, but that doesn't seem to be relevant for this case.
Tldr: is there some way to get the compiler to help out with missing return statements?
The easiest way to achieve this is to use implicit return, that way Nim will complain about branches missing a value:
type
X = ref Xobj
Xobj = object
x: int
Ret = ref RetObj
RetObj = object
y: int
proc fn(x: X): Ret =
result = if isNil(x): # just an example, any condition is fine
echo "ok"
else:
echo "ok too"
Ret(y:42)
var x: X
doAssert isNil(fn(x))