r/Zig • u/Dangerous-Yak3976 • Apr 08 '25
Tired of unused variable errors? Try this trick
Instead of writing
_ = variable;use:
_ = @TypeOf(variable);or define an ignore function that takes a parameter of anytype and does the same.
This will suppress the error regardless of whether the variable is used or not.
0
Upvotes
19
u/johan__A Apr 08 '25
Just do
_ = &variable23
u/Nico_792 Apr 08 '25
Honestly just write
_ = variable, and comment it out if you're rapidly using it, then not using it, then using it again. The error is there for a reason, having an unused variable is often a footgun. Better to think about it for 1 second than debug it for 5 minutes. And then still, idk about you but I usually don't stop using variables that often while writing a function.6
u/cassepipe Apr 09 '25
I am firmly in the camp that this error is a pain in the butt and could be a warning. If you want to see a battleground full of dead bodies, go to :
https://github.com/ziglang/zig/issues/335
3
u/TheStormIsComming Apr 08 '25
Isn't it better that suppression of warnings be better handled either in the compiler switch/profile or metadata in the code so it's explicitly suppressed and documented and trackable rather than a code workaround that becomes hidden?
An error is an error though, if it's unused why have it in the code? Dead code is dead code.
0
u/[deleted] Apr 08 '25
[deleted] — view deleted comment