Sobeston / zig.guide

Repo for https://zig.guide content. Get up to speed with Zig quickly.
https://zig.guide
MIT License
647 stars 170 forks source link

local variable is never mutated #233

Closed shiena closed 4 months ago

shiena commented 4 months ago

Since zig 0.12.0 there are several codes that give the local variable is never mutated error. For example

https://github.com/Sobeston/zig.guide/blob/master/website/versioned_docs/version-0.12/01-language-basics/23-optionals.md

test "orelse" {
    var a: ?f32 = null;
    var b = a orelse 0;
    try expect(b == 0);
    try expect(@TypeOf(b) == f32);
}

But don't worry. This can be avoided by using a code like _ = &a; as described by the PRs author. https://github.com/ziglang/zig/pull/18017

test "orelse" {
    var a: ?f32 = null;
    var b = a orelse 0;
    try expect(b == 0);
    try expect(@TypeOf(b) == f32);
    _ = &a;
    _ = &b;
}
Sobeston commented 4 months ago

Fixed this one in https://github.com/Sobeston/zig.guide/commit/51f80b5fc8f25aec2a470109ee3826ce1d8281da Thanks for the report, trying to fix the others now

shiena commented 4 months ago

@Sobeston Thanks for the correction. The following other code also causes the same compile error.

https://github.com/Sobeston/zig.guide/blob/master/website/versioned_docs/version-0.12/01-language-basics/24-comptime.md

https://github.com/Sobeston/zig.guide/blob/master/website/versioned_docs/version-0.12/01-language-basics/25-payload-captures.md

https://github.com/Sobeston/zig.guide/blob/master/website/versioned_docs/version-0.12/01-language-basics/28-anonymous-structs.md

https://github.com/Sobeston/zig.guide/blob/master/website/versioned_docs/version-0.12/01-language-basics/29-sentinel-termination.md

https://github.com/Sobeston/zig.guide/blob/master/website/versioned_docs/version-0.12/01-language-basics/30-vectors.md

Sobeston commented 4 months ago

Thanks again for the detailed report! Fixed in: