- When writing code in the 'Early return' style/pattern and using
return invalid, linter flags an error "not all code paths return a value"
- This happens because there is not another
return invalid at the end of the function.
- Given that
return invalid is the default return value (whether specified or not), I'm wondering/hoping if this lint rule can be updated to accommodate the early exit pattern without requiring us to add return invalid at the end of all our functions
- Note: if you use this pattern with a
sub function definition instead of function, the linter does not throw any warnings/errors
- I prefer to write these kind of early-exit functions using
sub, but in projects with named-function-style: "no-sub" that isn't possible
Link to the linter code/line that throws this warning/error
Example of early-return pattern:
' early-exit pattern using 'sub'
' passes lint
' this will NOT throw "not all code paths return a value"
sub myFunc (arg)
if arg = invalid then return
' do something with arg
' this function does not return anything
end function
' early-exit pattern using 'function'
' fails lint
' this will throw "not all code paths return a value"
function myFunc (arg)
if arg = invalid then return invalid
' do something with arg
' this function does not return anything (it is equivalent to a sub call definition)
end function
' early-exit pattern using 'function' with extra return
' passes lint
function myFunc (arg)
if arg = invalid then return invalid
' do something with arg
' this function does not return anything (it is equivalent to a sub call definition)
return invalid ' adding superfluous 'return invalid' makes lint happy
end function
If I can find the time and the bslint devs are open to changes to support this, I will try to submit a PR if I can get the time to do so
return invalid, linter flags an error "not all code paths return a value"return invalidat the end of the function.return invalidis the default return value (whether specified or not), I'm wondering/hoping if this lint rule can be updated to accommodate the early exit pattern without requiring us to addreturn invalidat the end of all our functionssubfunction definition instead offunction, the linter does not throw any warnings/errorssub, but in projects withnamed-function-style: "no-sub"that isn't possibleLink to the linter code/line that throws this warning/error
Example of early-return pattern:
If I can find the time and the bslint devs are open to changes to support this, I will try to submit a PR if I can get the time to do so