Skip to content

Erroneous "Not all code paths return a value" when using return invalid for early exits #135

@addison-adler

Description

@addison-adler
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions