Skip to content

Zig Error: unused local variable

error: unused local variable (and its twin, error: unused local constant) means you created a name inside a function and never used it. In most languages that’s a warning you can scroll past. Zig has no warnings at all: anything the compiler complains about stops the build. It’s probably the first Zig error you’ll meet, so let’s make it a quick one. For the other errors in this series, see Zig Errors Explained.

const std = @import("std");
pub fn main() void {
const name = "Ada";
var count: u32 = 0;
std.debug.print("Hello!\n", .{});
}
main.zig:5:9: error: unused local variable
var count: u32 = 0;
^~~~~
main.zig:4:11: error: unused local constant
const name = "Ada";
^~~~

Two names, two errors. count was made with var, so it’s a “local variable”. name was made with const, so it’s a “local constant”. The rule is the same for both. The caret (^~~~) points at the name, and the numbers after main.zig: are the line and column.

Notice the errors aren’t in line order: line 5 comes before line 4. Zig reports these in the order it finds them, so read every error in the list, not just the top one.

A local is a name made inside a function or block with const or var. Zig checks that every local is used at least once before the block ends. The check is simple: any mention of the name after it’s declared counts, even assigning to it. So count = 5; on its own is enough to make the error go away, even though nothing ever looks at the value. Don’t rely on that: a value nobody reads is still a bug, the compiler just can’t see it.

Why be so strict? A name you never use is often a sign of a real bug: you computed something and forgot to print it, or you used the wrong name further down. Zig would rather stop you now than let that slip into a program. The 0.9.0 release notes, where this became an error, give the reasons: it helps when you rework code, and it prevents bugs. The Zig Variables lesson covers the rule along with const and var.

Most of the time the error is right: you meant to use the name and forgot. Here we meant to greet name and show count:

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

Output:

Hello, Ada! Visit number 1.

Once both names are used in the print call, the errors are gone. If the unused name is something you just computed, like a total, that’s a hint the program forgot to report its result.

If you don’t need the name any more (maybe it’s left over from an older version of the code), delete the line. Less code is the best fix there is:

const std = @import("std");
pub fn main() void {
std.debug.print("Hello!\n", .{});
}

Output:

Hello!

Fix 3: Discard it with _ = name; while you work

Section titled “Fix 3: Discard it with _ = name; while you work”

Sometimes you’re halfway through a function and want to run it before every name is used. Assigning a value to _ tells Zig “I know, I’m not using this yet”:

const std = @import("std");
pub fn main(init: std.process.Init) void {
const gpa = init.gpa;
_ = gpa; // TODO: use this for the list later
std.debug.print("Not done yet\n", .{});
}

Output:

Not done yet

This is handy, but treat it as a sticky note, not a fix. Once you start using gpa, you must delete the _ = gpa; line, or you’ll get the next error in this series: pointless discard of local constant.

The same rule applies to the names between |...| in a for, while, if or switch. There the error says unused capture:

const std = @import("std");
pub fn main() void {
const items = [_]i32{ 4, 5 };
for (items, 0..) |item, i| {
std.debug.print("{d}\n", .{item});
}
}
main.zig:5:29: error: unused capture
for (items, 0..) |item, i| {
^

We asked for the index i and never read it. Either drop the 0.. and the i (the loop is just for (items) |item|), or write _ in that spot when you need the position but not the value:

const std = @import("std");
pub fn main() void {
const items = [_]i32{ 4, 5 };
for (items, 0..) |_, i| {
std.debug.print("position {d}\n", .{i});
}
}

Output:

position 0
position 1

The Zig For Loops lesson covers loops with an index.

  • Half-written code. You declared three names, wrote the code for one, and ran it to check. Fix 3 is made for this.
  • After a refactor. You moved some logic into a function and left the old const behind. Delete it.
  • const gpa = init.gpa; at the top of main in a program that doesn’t allocate yet.
  • A result you forgot to print, like const total = add(a, b); with no line that shows total.
  • Function parameters have their own version of this: unused function parameter.

This program should print sum: 60, but it doesn’t build. Fix it without deleting anything.

const std = @import("std");
pub fn main() void {
const prices = [_]u32{ 10, 20, 30 };
var total: u32 = 0;
for (prices) |price| {
total += price;
}
const message = "sum";
std.debug.print("done\n", .{});
}

Hint: only one name gives an error, but the program has a second problem: it never shows its result. Put both names in the last print.

Show solution
const std = @import("std");
pub fn main() void {
const prices = [_]u32{ 10, 20, 30 };
var total: u32 = 0;
for (prices) |price| {
total += price;
}
const message = "sum";
std.debug.print("{s}: {d}\n", .{ message, total });
}

Output:

sum: 60

Only message gave an error: total += price mentions total, so the compiler counted it as used. But the program still never showed the total. The compiler catches names you forgot completely, not values you forgot to report, so read your own code with that in mind.

  • Zig has no warnings: an unused local is a compile error.
  • unused local variable is for var, unused local constant for const, unused capture for |x|.
  • Any mention of the name counts as a use, even x = 5;. A value nobody reads is still a bug the compiler can’t see.
  • Fix it by using the name, deleting it, or writing _ = name; while you’re still working.
  • Remove _ = name; once you use the name, or you get pointless discard.

Next up: local variable is never mutated, the error for a var that could have been a const.

Quick check

1. You wrote const total = add(2, 3); and nothing else in the function mentions total. What happens?
2. What does _ = gpa; do?
3. A loop for (names, 0..) |name, i| only prints i. Which change fixes unused capture?