Skip to content

Detect functions that never return (and @noreturn annotation) #1881

@CelticMinstrel

Description

@CelticMinstrel

Some functions, such as error, never return control to the caller. This can be used to prove that a value is not nil, for example:

function my_function(input)
  local value = input.value or error('Missing value')
  -- The following line would currently raise a "Needs nil check" warning
  -- However, if it was nil at runtime, an error would have been raised and it would never get this far.
  return value:sub(1, 3)
end

It's not just error that can cause this, however. A custom function could be written that never returns as well (the most obvious way is that the function unconditionally calls error, but there may be other ways involving coroutines or functions implemented in C). A @noreturn annotation could be used to indicate this, similar to @nodiscard.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions