From ce938c864604155fc081dab017867e7e461c61cc Mon Sep 17 00:00:00 2001 From: Jakub Date: Mon, 25 Mar 2024 22:53:09 +0100 Subject: Detect discard-returns in all block types --- script/core/diagnostics/discard-returns.lua | 4 +- test/diagnostics/discard-returns.lua | 246 ++++++++++++++++++++++++++++ 2 files changed, 247 insertions(+), 3 deletions(-) diff --git a/script/core/diagnostics/discard-returns.lua b/script/core/diagnostics/discard-returns.lua index cef7ece5..9e573596 100644 --- a/script/core/diagnostics/discard-returns.lua +++ b/script/core/diagnostics/discard-returns.lua @@ -12,9 +12,7 @@ return function (uri, callback) end ---@async guide.eachSourceType(state.ast, 'call', function (source) - local parent = source.parent - if parent.type ~= 'function' - and parent.type ~= 'main' then + if not guide.isBlockType(source.parent) then return end await.delay() diff --git a/test/diagnostics/discard-returns.lua b/test/diagnostics/discard-returns.lua index 2e348390..5acd6146 100644 --- a/test/diagnostics/discard-returns.lua +++ b/test/diagnostics/discard-returns.lua @@ -15,3 +15,249 @@ end X = f() ]] + +TEST [[ +---@nodiscard +local function f() + return 1 +end + +for i = 1, 2 do + +end +]] + +TEST [[ +---@nodiscard +local function f() + return 1 +end + +for i = 1, 2 do + local v = f() +end +]] + +TEST [[ +---@nodiscard +local function f() + return 1 +end + +while true do + + break +end +]] + +TEST [[ +---@nodiscard +local function f() + return 1 +end + +while true do + local v = f() + break +end +]] + +TEST [[ +---@nodiscard +local function f() + return 1 +end + +repeat + + break +until true +]] + +TEST [[ +---@nodiscard +local function f() + return 1 +end + +repeat + local v = f() + break +until true +]] + +TEST [[ +---@nodiscard +local function f() + return 1 +end + +for index, value in ipairs({}) do + +end +]] + +TEST [[ +---@nodiscard +local function f() + return 1 +end + +for index, value in ipairs({}) do + local v = f() +end +]] + +TEST [[ +---@nodiscard +local function f() + return 1 +end + +if 1 == 1 then + +end +]] + +TEST [[ +---@nodiscard +local function f() + return 1 +end + +if 1 == 1 then + local v = f() +end +]] + +TEST [[ +---@nodiscard +local function f() + return 1 +end + +if 1 == 1 then + local v = f() +else + +end +]] + +TEST [[ +---@nodiscard +local function f() + return 1 +end + +if 1 == 1 then + local v = f() +else + local v = f() +end +]] + +TEST [[ +---@nodiscard +local function f() + return 1 +end + +if 1 == 1 then + local v = f() +elseif 1 == 2 then + +else + local v = f() +end +]] + +TEST [[ +---@nodiscard +local function f() + return 1 +end + +if 1 == 1 then + local v = f() +elseif 1 == 2 then + local v = f() +else + local v = f() +end +]] + +TEST [[ +---@nodiscard +local function f() + return 1 +end + +local function bar(callback) +end + +bar(function () + +end) +]] + +TEST [[ +---@nodiscard +local function f() + return 1 +end + +local function bar(callback) +end + +bar(function () + local v = f() +end) +]] + +TEST [[ +---@nodiscard +local function f() + return 1 +end + +do + +end +]] + +TEST [[ +---@nodiscard +local function f() + return 1 +end + +do + local v = f() +end +]] + +TEST [[ +---@nodiscard +local function f() + return 2 +end + +for i = 1, f() do +end +]] + +TEST [[ +---@nodiscard +local function list_iter(t) + local i = 0 + local n = #t + return function () + i = i + 1 + if i <= n then return t[i] end + end +end + +local t = {10, 20, 30} +for element in list_iter(t) do +end +]] -- cgit v1.2.3