lua-language-server icon indicating copy to clipboard operation
lua-language-server copied to clipboard

feature request: attach a `reason` parameter to `@nodiscard`

Open thacuber2a03 opened this issue 5 months ago • 4 comments

[!NOTE] I couldn't find an issue for this. sorry if there's a duplicate.

I propose updating the @nodiscard annotation to add a "reason" as to why not discard the value:

---@nodiscard [<reason>]

this would mirror Rust's #[must_use = "reason"] annotation.

example:

---Creates a new class that inherits from this one.
---@return Class
---@nodiscard This method returns the new class; it's pointless to immediately discard it.
function Object:extend()
  -- ...
end

discarding the output would then display the warning:

* [warning][Lua Diagnostics.][discard-returns]
  The return values of this function cannot be discarded:
  This method returns the new class; it's pointless to immediately discard it.

note the colon at the end of "discarded". if not given a reason, it would fall-back to the usual message, with "discarded" ending with a dot and no further explanation (yes, I'm aware that reason in the Object:extend example isn't exactly... professional 😅)

thacuber2a03 avatar May 04 '25 16:05 thacuber2a03