Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ignore "local variable is never mutated" error if ignoring with "_ = myvar" #20584

Closed
silbinarywolf opened this issue Jul 11, 2024 · 5 comments
Closed
Labels
error message This issue points out an error message that is unhelpful and should be improved.

Comments

@silbinarywolf
Copy link
Contributor

silbinarywolf commented Jul 11, 2024

Zig Version

0.13.0

Steps to Reproduce and Observed Output

var myblah = 5; // <- "local variable is never mutated" error occurs
_ = myblah; // autofix

Expected Output

If I'm purposefully ignoring using a variable for now, I don't want it to throw an error as I then need to change it to const, then remove the ignore statement, then revert from const to var while I'm prototyping things.

var myblah = 5; // <- no error should occur
_ = myblah;
@silbinarywolf silbinarywolf added the error message This issue points out an error message that is unhelpful and should be improved. label Jul 11, 2024
@Rexicon226
Copy link
Contributor

Not a bug. The intended way to discard vars is with _ = &myblah;.

@rohlem
Copy link
Contributor

rohlem commented Jul 11, 2024

(as pointed out above)
This is currently only mentioned via inline comments in the langref, in completely unrelated tests (about vectors, pointer arithmetic, slicing, allowzero).

The current compile error has a note: consider using 'const'.
We could additionally add notes use '_ = &x;' to suppress this error (and use '_ = x;' to suppress this error for the const case) to make it easier for people to find out about this mechanism.
Not sure what the opinion of maintainers on this is - we don't want to encourage unclean code, but IMO these features currently aren't very discoverable.
(We could also, or instead, add a section on "suppressing errors" to the langref.)

@mlugg
Copy link
Member

mlugg commented Jul 11, 2024

_ = &x is a dirty secret, and not to be encouraged. There's no intended workaround in this case: just make your variable const. The only reason we ever use the _ = &x trick in tests is to ensure something is runtime-known to demonstrate runtime behaviour.

Even in prototyping, what is your use case for this?

@silbinarywolf
Copy link
Contributor Author

The use case is usually prototyping new behaviour within a game loop, so putting a new variable at the top of the loop:

var some_variable_we_want_to_retain_across_frames: [256]u8 | i32 // could be a buffer, an int, a struct, whatever
while (!has_game_exited) {

Then deeper within this large game-loop, hundreds of lines below, I'm making this variable be used for something.

  • If its a buffer, maybe its an ImGui text field I'm testing out.
  • If it's an int/Timer, maybe im tracking time elapsed.
  • If its a struct, maybe its a new entity, like a moving platform and I'm just testing it out before pulling it out into an ArrayList of that type/etc.

With Zig, the unused variable rule was initially not a big issue for me because ZLS could auto-generate the unused logic with _ = myvar; // autofix. This was until requiring const came in and broke that functionality.

@rohlem
Copy link
Contributor

rohlem commented Jul 12, 2024

initially not a big issue for me [due to] ZLS [...] until requiring const came in and broke that functionality.

I would suggest creating an issue for an autofix _ = &x; in ZLS (surprisingly I couldn't find one already existing);
it doesn't seem like upstream Zig wants to improve usability in this regard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
error message This issue points out an error message that is unhelpful and should be improved.
Projects
None yet
Development

No branches or pull requests

5 participants