zigtools / zls

A Zig language server supporting Zig developers with features like autocomplete and goto definition
MIT License
2.86k stars 290 forks source link

Unused variables should be detected by the language server #2016

Closed maclong9 closed 1 month ago

maclong9 commented 1 month ago

Zig Version

0.13.0

ZLS Version

0.14.0-dev.135+b481aae

Client / Code Editor / Extensions

Helix Editor

Steps to Reproduce and Observed Behavior

print("defer means x inside the block is equal to\n", .{x});

The above when compiled returns the following error:

error: unused argument in 'defer means x inside the block is equal to
                                                                                    '
            1 => @compileError("unused argument in '" ++ fmt ++ "'"),
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
    print__anon_2902: /Users/maclong/.local/share/mise/installs/zig/0.13.0/lib/std/io/Writer.zig:24:26
    print: /Users/maclong/.local/share/mise/installs/zig/0.13.0/lib/std/io.zig:324:47
    remaining reference traces hidden; use '-freference-trace' to see all reference traces

It should be displayed in the editor before compiling.

Expected Behavior

Error should display as an error in editor so we can easily catch these errors before compilation.

Relevant log output

No response

xdBronch commented 1 month ago

doing this is very hard if not effectively impossible, at least correctly. std.fmt relies heavily on comptime which zls doesnt support yet, its the same reason there arent proper type errors in general. in the meantime you can try the build_on_save feature, heres a nice article to get it setup https://kristoff.it/blog/improving-your-zls-experience/

Techatrix commented 1 month ago

This issue is a very specific example of #2017 + #2018. There is no need to track this separately so I will be closing this.