298298 test_hover (" local a = |(1)" , " 1" )
299299 test_hover (" local a = (|1)" , " 1" )
300300end
301+
302+ -- Test unreachable code is dimmed via Unnecessary diagnostic tag
303+ do
304+ local client = LSPClient .New ()
305+ local root_uri = " file:///workspace"
306+ client :SetWorkingDirectory (" /workspace" )
307+ client :Initialize (lsp , root_uri )
308+ local file_uri = root_uri .. " /test.nlua"
309+ local code = [[
310+ if false then
311+ local www = "hello"
312+ end
313+ ]]
314+ client :ClearNotifications ()
315+ client :Notify (
316+ lsp ,
317+ " textDocument/didOpen" ,
318+ {
319+ textDocument = {
320+ uri = file_uri ,
321+ languageId = " nattlua" ,
322+ version = 1 ,
323+ text = code ,
324+ },
325+ }
326+ )
327+ local diag_notifications = client :GetNotifications (" textDocument/publishDiagnostics" )
328+ assert (# diag_notifications > 0 , " Should have publishDiagnostics notifications" )
329+ local found_unnecessary = false
330+
331+ for _ , notif in ipairs (diag_notifications ) do
332+ for _ , diag in ipairs (notif .params .diagnostics ) do
333+ if diag .tags then
334+ for _ , tag in ipairs (diag .tags ) do
335+ if tag == 1 and diag .message :find (" unreachable" ) then
336+ found_unnecessary = true
337+ end
338+ end
339+ end
340+ end
341+ end
342+
343+ assert (
344+ found_unnecessary ,
345+ " Unreachable code inside 'if false' should produce a diagnostic with Unnecessary tag (1)"
346+ )
347+ end
348+
349+ -- Test unreachable code in else-branch when condition is always true
350+ do
351+ local client = LSPClient .New ()
352+ local root_uri = " file:///workspace"
353+ client :SetWorkingDirectory (" /workspace" )
354+ client :Initialize (lsp , root_uri )
355+ local file_uri = root_uri .. " /test.nlua"
356+ local code = [[
357+ local x = true
358+ if x then
359+ local alive = "reachable"
360+ else
361+ local dead = "unreachable"
362+ end
363+ ]]
364+ client :ClearNotifications ()
365+ client :Notify (
366+ lsp ,
367+ " textDocument/didOpen" ,
368+ {
369+ textDocument = {
370+ uri = file_uri ,
371+ languageId = " nattlua" ,
372+ version = 1 ,
373+ text = code ,
374+ },
375+ }
376+ )
377+ local diag_notifications = client :GetNotifications (" textDocument/publishDiagnostics" )
378+ assert (# diag_notifications > 0 , " Should have publishDiagnostics notifications" )
379+ local found_unnecessary = false
380+
381+ for _ , notif in ipairs (diag_notifications ) do
382+ for _ , diag in ipairs (notif .params .diagnostics ) do
383+ if diag .tags then
384+ for _ , tag in ipairs (diag .tags ) do
385+ if tag == 1 and diag .message :find (" unreachable" ) then
386+ found_unnecessary = true
387+ end
388+ end
389+ end
390+ end
391+ end
392+
393+ assert (
394+ found_unnecessary ,
395+ " Unreachable else-branch when condition is always true should produce a diagnostic with Unnecessary tag (1)"
396+ )
397+ end
0 commit comments