Skip to content

Commit

Permalink
fix new "var never mutated" error on Zig 0.12.0-dev.1663+6b1a823b2
Browse files Browse the repository at this point in the history
Fixes these errors (introduced in ziglang/zig#18017
and ziglang/zig@6b1a823 ):

```
src/main.zig:290:13: error: local variable is never mutated
        var line_ = line_fbs.getWritten();
            ^~~~~
src/main.zig:290:13: note: consider using 'const'
src/main.zig:450:17: error: local variable is never mutated
            var path = std.fs.path.joinZ(allocator, &.{p, "ncdu", "config"}) catch unreachable;
                ^~~~
src/main.zig:450:17: note: consider using 'const'

...
```

Will be included in future Zig 0.12, this fix is backward compatible:
ncdu still builds and runs fine on Zig 0.11.0.

Signed-off-by: Eric Joldasov <bratishkaerik@getgoogleoff.me>
  • Loading branch information
BratishkaErik committed Nov 20, 2023
1 parent 115de25 commit c83159f
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/browser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,8 @@ const info = struct {
var links_idx: usize = 0;

fn lt(_: void, a: *model.Link, b: *model.Link) bool {
var pa = a.path(false);
var pb = b.path(false);
const pa = a.path(false);
const pb = b.path(false);
defer main.allocator.free(pa);
defer main.allocator.free(pb);
return std.mem.lessThan(u8, pa, pb);
Expand Down
10 changes: 5 additions & 5 deletions src/exclude.zig
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const Pattern = struct {

fn parse(pat_: []const u8) *const Pattern {
var pat = std.mem.trimLeft(u8, pat_, "/");
var top = main.allocator.create(Pattern) catch unreachable;
const top = main.allocator.create(Pattern) catch unreachable;
var tail = top;
tail.sub = null;
while (std.mem.indexOfScalar(u8, pat, '/')) |idx| {
Expand Down Expand Up @@ -147,7 +147,7 @@ fn PatternList(comptime withsub: bool) type {
fn append(self: *Self, pat: *const Pattern) void {
std.debug.assert((pat.sub != null) == withsub);
if (pat.isliteral) {
var e = self.literals.getOrPut(main.allocator, pat) catch unreachable;
const e = self.literals.getOrPut(main.allocator, pat) catch unreachable;
if (!e.found_existing) {
e.key_ptr.* = pat;
e.value_ptr.* = if (withsub) .{} else {};
Expand Down Expand Up @@ -250,16 +250,16 @@ pub fn getPatterns(path_: []const u8) Patterns {
var pat = root;
defer pat.deinit();
while (std.mem.indexOfScalar(u8, path, '/')) |idx| {
var name = main.allocator.dupeZ(u8, path[0..idx]) catch unreachable;
const name = main.allocator.dupeZ(u8, path[0..idx]) catch unreachable;
defer main.allocator.free(name);
path = path[idx+1..];

var sub = pat.enter(name);
const sub = pat.enter(name);
pat.deinit();
pat = sub;
}

var name = main.allocator.dupeZ(u8, path) catch unreachable;
const name = main.allocator.dupeZ(u8, path) catch unreachable;
defer main.allocator.free(name);
return pat.enter(name);
}
Expand Down
12 changes: 6 additions & 6 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ fn tryReadArgsFile(path: [:0]const u8) void {
error.EndOfStream => if (line_fbs.getPos() catch unreachable == 0) break,
else => |e| ui.die("Error reading from {s}: {s}\nRun with --ignore-config to skip reading config files.\n", .{ path, ui.errorString(e) }),
};
var line_ = line_fbs.getWritten();
const line_ = line_fbs.getWritten();

var line = std.mem.trim(u8, line_, &std.ascii.whitespace);
if (line.len == 0 or line[0] == '#') continue;
Expand Down Expand Up @@ -447,11 +447,11 @@ pub fn main() void {
tryReadArgsFile("/etc/ncdu.conf");

if (std.os.getenvZ("XDG_CONFIG_HOME")) |p| {
var path = std.fs.path.joinZ(allocator, &.{p, "ncdu", "config"}) catch unreachable;
const path = std.fs.path.joinZ(allocator, &.{p, "ncdu", "config"}) catch unreachable;
defer allocator.free(path);
tryReadArgsFile(path);
} else if (std.os.getenvZ("HOME")) |p| {
var path = std.fs.path.joinZ(allocator, &.{p, ".config", "ncdu", "config"}) catch unreachable;
const path = std.fs.path.joinZ(allocator, &.{p, ".config", "ncdu", "config"}) catch unreachable;
defer allocator.free(path);
tryReadArgsFile(path);
}
Expand All @@ -462,7 +462,7 @@ pub fn main() void {
var export_file: ?[:0]const u8 = null;
var quit_after_scan = false;
{
var arglist = std.process.argsAlloc(allocator) catch unreachable;
const arglist = std.process.argsAlloc(allocator) catch unreachable;
defer std.process.argsFree(allocator, arglist);
var args = Args.init(arglist);
_ = args.next(); // program name
Expand Down Expand Up @@ -506,7 +506,7 @@ pub fn main() void {
event_delay_timer = std.time.Timer.start() catch unreachable;
defer ui.deinit();

var out_file = if (export_file) |f| (
const out_file = if (export_file) |f| (
if (std.mem.eql(u8, f, "-")) stdout
else std.fs.cwd().createFileZ(f, .{})
catch |e| ui.die("Error opening export file: {s}.\n", .{ui.errorString(e)})
Expand Down Expand Up @@ -573,7 +573,7 @@ pub fn handleEvent(block: bool, force_draw: bool) void {

var firstblock = block;
while (true) {
var ch = ui.getch(firstblock);
const ch = ui.getch(firstblock);
if (ch == 0) return;
if (ch == -1) return handleEvent(firstblock, true);
switch (state) {
Expand Down
4 changes: 2 additions & 2 deletions src/model.zig
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ pub const devices = struct {
var lookup = std.AutoHashMap(u64, DevId).init(main.allocator);

pub fn getId(dev: u64) DevId {
var d = lookup.getOrPut(dev) catch unreachable;
const d = lookup.getOrPut(dev) catch unreachable;
if (!d.found_existing) {
d.value_ptr.* = @as(DevId, @intCast(list.items.len));
list.append(dev) catch unreachable;
Expand Down Expand Up @@ -400,7 +400,7 @@ pub const inodes = struct {
nlink += 1;
var parent: ?*Dir = it.parent;
while (parent) |p| : (parent = p.parent) {
var de = dirs.getOrPut(p) catch unreachable;
const de = dirs.getOrPut(p) catch unreachable;
if (de.found_existing) de.value_ptr.* += 1
else de.value_ptr.* = 1;
}
Expand Down
4 changes: 2 additions & 2 deletions src/scan.zig
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ const Context = struct {
wr.print("{d}", .{std.time.timestamp()}) catch |e| writeErr(e);
wr.writeByte('}') catch |e| writeErr(e);

var self = main.allocator.create(Self) catch unreachable;
const self = main.allocator.create(Self) catch unreachable;
self.* = .{ .wr = buf };
return self;
}
Expand Down Expand Up @@ -555,7 +555,7 @@ pub fn setupRefresh(parent: *model.Dir) void {
// To be called after setupRefresh() (or from scanRoot())
pub fn scan() void {
defer active_context.deinit();
var dir_ = std.fs.cwd().openDirZ(active_context.pathZ(), .{}, true) catch |e| {
const dir_ = std.fs.cwd().openDirZ(active_context.pathZ(), .{}, true) catch |e| {
active_context.last_error = main.allocator.dupeZ(u8, active_context.path.items) catch unreachable;
active_context.fatal_error = e;
while (main.state == .refresh or main.state == .scan)
Expand Down
4 changes: 2 additions & 2 deletions src/ui.zig
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,9 @@ pub fn init() void {
if (inited) return;
clearScr();
if (main.config.nc_tty) {
var tty = c.fopen("/dev/tty", "r+");
const tty = c.fopen("/dev/tty", "r+");
if (tty == null) die("Error opening /dev/tty: {s}.\n", .{ c.strerror(@intFromEnum(std.c.getErrno(-1))) });
var term = c.newterm(null, tty, tty);
const term = c.newterm(null, tty, tty);
if (term == null) die("Error initializing ncurses.\n", .{});
_ = c.set_term(term);
} else {
Expand Down

0 comments on commit c83159f

Please sign in to comment.