Skip to content

Zig Error: pointless discard of local variable

error: pointless discard of local variable means you have a line like _ = count; that says “I’m not using count”, and then you do use count. The two disagree, so Zig asks you to delete the discard. It’s one of the easiest errors to fix: remove one line. It comes in three flavours, local variable, local constant and function parameter, all with the same fix. For the other errors in this series, see Zig Errors Explained.

const std = @import("std");
pub fn main() void {
var count: u32 = 0;
_ = count;
count += 1;
std.debug.print("{d}\n", .{count});
}
main.zig:5:9: error: pointless discard of local variable
_ = count;
^~~~~
main.zig:7:32: note: used here
std.debug.print("{d}\n", .{count});
^~~~~

The error points at the discard, and the note points at a place where you really use the name. Here that’s the print on line 7, but count += 1 on line 6 counts as a use too: any mention of the name does.

_ = x; assigns x to _, which throws the value away. It’s the way to silence an unused local variable error while you’re still writing a function. Once you use x, the discard has no job left, and Zig doesn’t let pointless code sit around. Leaving it would also mislead the next reader into thinking x isn’t used. The 0.9.0 release notes, where unused locals became errors, explain where _ = x; comes from.

This error usually comes from a two-step history: you wrote _ = x; (or your editor added it for you), then later wrote the code that uses x and forgot the placeholder.

That’s it. Remove _ = count;:

const std = @import("std");
pub fn main() void {
var count: u32 = 0;
count += 1;
std.debug.print("{d}\n", .{count});
}

Output:

1

Fix 2: Same fix for constants and parameters

Section titled “Fix 2: Same fix for constants and parameters”

The message changes with the kind of name, but the fix doesn’t. A parameter gives pointless discard of function parameter:

const std = @import("std");
fn show(label: []const u8) void {
_ = label;
std.debug.print("{s}\n", .{label});
}
pub fn main() void {
show("hi");
}
main.zig:4:9: error: pointless discard of function parameter
_ = label;
^~~~~
main.zig:5:32: note: used here
std.debug.print("{s}\n", .{label});
^~~~~

Delete line 4:

const std = @import("std");
fn show(label: []const u8) void {
std.debug.print("{s}\n", .{label});
}
pub fn main() void {
show("hi");
}

Output:

hi

A const gives pointless discard of local constant. Same story.

You’ll see a different line in our lessons and in the Zig docs: _ = &x;, with an &. That one isn’t flagged as pointless:

const std = @import("std");
pub fn main() void {
var count: u32 = 0;
_ = &count;
count += 1;
std.debug.print("{d}\n", .{count});
}

Output:

1

&count takes the address of count, which tells Zig “this might be changed through a pointer”. It’s used to stop local variable is never mutated in small examples and tests, where a value has to stay a runtime variable on purpose. In this program it does nothing useful (the += already changes count), so you’d delete it too. Just know it’s not the same thing as _ = count;.

  • After filling in a function you started with placeholder discards.
  • After accepting an editor quick fix that added _ = x;, then using x a few lines later.
  • In main(init) when you wrote _ = init; and later started using init.gpa or init.io.
  • In callbacks, where you discarded a parameter and later needed it.

This function was written in two steps and still has its placeholders. Make it build and print Ada: 3 points.

const std = @import("std");
fn report(name: []const u8, points: u32, bonus: u32) void {
_ = name;
_ = points;
_ = bonus;
std.debug.print("{s}: {d} points\n", .{ name, points });
}
pub fn main() void {
report("Ada", 3, 0);
}

Hint: two discards are pointless, one isn’t. Which parameter is never used?

Show solution
const std = @import("std");
fn report(name: []const u8, points: u32, bonus: u32) void {
_ = bonus;
std.debug.print("{s}: {d} points\n", .{ name, points });
}
pub fn main() void {
report("Ada", 3, 0);
}

Output:

Ada: 3 points

name and points are used in print, so their discards go. bonus is still unused, so its discard stays (or you could remove the parameter and the 0 in the call).

  • The error means a _ = x; line is left over: you discard x and use it too.
  • The fix is to delete the discard line.
  • It says local variable, local constant or function parameter depending on the name; the fix is the same.
  • The note: used here shows a place where you use the name. Any mention counts, even x = 5;.
  • _ = &x; is a different trick and isn’t flagged.

Next up: use of undeclared identifier ‘x’, the error for a name Zig can’t find.

Quick check

1. What is the fix for pointless discard of local constant?
2. You wrote _ = total; and later total += 5;. Is the discard pointless?
3. What does the note: used here line point at?