authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-06-23 11:32:28-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-06-23 11:32:28-07:00
log32595320807fdb8a175674621b0b2216527f9d4f
treed9d2816380013ea68d6a2065e58c0a725d7235c7
parentde9306096e17519dfb730457059ce08496e5f758

langref: fix unused vars


1 files changed, 54 insertions(+), 24 deletions(-)

doc/langref.html.in+54-24
......@@ -965,7 +965,7 @@ test "thread local storage" {
965965 thread2.wait();
966966}
967967
968fn testTls(context: void) void {
968fn testTls(_: void) void {
969969 assert(x == 1234);
970970 x += 1;
971971 assert(x == 1235);
......@@ -2502,6 +2502,8 @@ test "struct namespaced variable" {
25022502
25032503 // you can still instantiate an empty struct
25042504 const does_nothing = Empty {};
2505
2506 _ = does_nothing;
25052507}
25062508
25072509// struct field order is determined by the compiler for optimal performance.
......@@ -3026,11 +3028,12 @@ const Foo = enum { a, b, c };
30263028export fn entry(foo: Foo) void { }
30273029 {#code_end#}
30283030 <p>
3029 For a C-ABI-compatible enum, use {#syntax#}extern enum{#endsyntax#}:
3031 For a C-ABI-compatible enum, provide an explicit tag type to
3032 the enum:
30303033 </p>
30313034 {#code_begin|obj#}
3032const Foo = extern enum { a, b, c };
3033export fn entry(foo: Foo) void { }
3035const Foo = enum(c_int) { a, b, c };
3036export fn entry(foo: Foo) void { _ = foo; }
30343037 {#code_end#}
30353038 {#header_close#}
30363039
......@@ -3392,9 +3395,11 @@ test "inside test block" {
33923395test "separate scopes" {
33933396 {
33943397 const pi = 3.14;
3398 _ = pi;
33953399 }
33963400 {
33973401 var pi: bool = true;
3402 _ = pi;
33983403 }
33993404}
34003405 {#code_end#}
......@@ -3432,7 +3437,7 @@ test "switch simple" {
34323437 // Switching on arbitrary expressions is allowed as long as the
34333438 // expression is known at compile-time.
34343439 zz => zz,
3435 comptime blk: {
3440 blk: {
34363441 const d: u32 = 5;
34373442 const e: u32 = 100;
34383443 break :blk d + e;
......@@ -3831,7 +3836,7 @@ test "for basics" {
38313836 // To access the index of iteration, specify a second capture value.
38323837 // This is zero-indexed.
38333838 var sum2: i32 = 0;
3834 for (items) |value, i| {
3839 for (items) |_, i| {
38353840 try expect(@TypeOf(i) == usize);
38363841 sum2 += @intCast(i32, i);
38373842 }
......@@ -3984,7 +3989,7 @@ test "if optional" {
39843989 }
39853990
39863991 const b: ?u32 = null;
3987 if (b) |value| {
3992 if (b) |_| {
39883993 unreachable;
39893994 } else {
39903995 try expect(true);
......@@ -4021,11 +4026,13 @@ test "if error union" {
40214026 if (a) |value| {
40224027 try expect(value == 0);
40234028 } else |err| {
4029 _ = err;
40244030 unreachable;
40254031 }
40264032
40274033 const b: anyerror!u32 = error.BadValue;
40284034 if (b) |value| {
4035 _ = value;
40294036 unreachable;
40304037 } else |err| {
40314038 try expect(err == error.BadValue);
......@@ -4045,13 +4052,13 @@ test "if error union" {
40454052 var c: anyerror!u32 = 3;
40464053 if (c) |*value| {
40474054 value.* = 9;
4048 } else |err| {
4055 } else |_| {
40494056 unreachable;
40504057 }
40514058
40524059 if (c) |value| {
40534060 try expect(value == 9);
4054 } else |err| {
4061 } else |_| {
40554062 unreachable;
40564063 }
40574064}
......@@ -4064,18 +4071,20 @@ test "if error union with optional" {
40644071 if (a) |optional_value| {
40654072 try expect(optional_value.? == 0);
40664073 } else |err| {
4074 _ = err;
40674075 unreachable;
40684076 }
40694077
40704078 const b: anyerror!?u32 = null;
40714079 if (b) |optional_value| {
40724080 try expect(optional_value == null);
4073 } else |err| {
4081 } else |_| {
40744082 unreachable;
40754083 }
40764084
40774085 const c: anyerror!?u32 = error.BadValue;
40784086 if (c) |optional_value| {
4087 _ = optional_value;
40794088 unreachable;
40804089 } else |err| {
40814090 try expect(err == error.BadValue);
......@@ -4087,13 +4096,13 @@ test "if error union with optional" {
40874096 if (optional_value.*) |*value| {
40884097 value.* = 9;
40894098 }
4090 } else |err| {
4099 } else |_| {
40914100 unreachable;
40924101 }
40934102
40944103 if (d) |optional_value| {
40954104 try expect(optional_value.? == 9);
4096 } else |err| {
4105 } else |_| {
40974106 unreachable;
40984107 }
40994108}
......@@ -4246,6 +4255,7 @@ test "type of unreachable" {
42464255 {#code_begin|test#}
42474256fn foo(condition: bool, b: u32) void {
42484257 const a = if (condition) b else return;
4258 _ = a;
42494259 @panic("do something with a");
42504260}
42514261test "noreturn" {
......@@ -4574,7 +4584,7 @@ test "parse u64" {
45744584 {#code_begin|syntax#}
45754585fn doAThing(str: []u8) void {
45764586 const number = parseU64(str, 10) catch 13;
4577 // ...
4587 _ = number; // ...
45784588}
45794589 {#code_end#}
45804590 <p>
......@@ -4589,7 +4599,7 @@ fn doAThing(str: []u8) void {
45894599 {#code_begin|syntax#}
45904600fn doAThing(str: []u8) !void {
45914601 const number = parseU64(str, 10) catch |err| return err;
4592 // ...
4602 _ = number; // ...
45934603}
45944604 {#code_end#}
45954605 <p>
......@@ -4598,7 +4608,7 @@ fn doAThing(str: []u8) !void {
45984608 {#code_begin|syntax#}
45994609fn doAThing(str: []u8) !void {
46004610 const number = try parseU64(str, 10);
4601 // ...
4611 _ = number; // ...
46024612}
46034613 {#code_end#}
46044614 <p>
......@@ -5022,7 +5032,7 @@ extern fn malloc(size: size_t) ?*u8;
50225032
50235033fn doAThing() ?*Foo {
50245034 const ptr = malloc(1234) orelse return null;
5025 // ...
5035 _ = ptr; // ...
50265036}
50275037 {#code_end#}
50285038 <p>
......@@ -5135,6 +5145,7 @@ test "optional pointers" {
51355145test "type coercion - variable declaration" {
51365146 var a: u8 = 1;
51375147 var b: u16 = a;
5148 _ = b;
51385149}
51395150
51405151test "type coercion - function call" {
......@@ -5142,11 +5153,14 @@ test "type coercion - function call" {
51425153 foo(a);
51435154}
51445155
5145fn foo(b: u16) void {}
5156fn foo(b: u16) void {
5157 _ = b;
5158}
51465159
51475160test "type coercion - @as builtin" {
51485161 var a: u8 = 1;
51495162 var b = @as(u16, a);
5163 _ = b;
51505164}
51515165 {#code_end#}
51525166 <p>
......@@ -5174,7 +5188,7 @@ test "type coercion - const qualification" {
51745188 foo(b);
51755189}
51765190
5177fn foo(a: *const i32) void {}
5191fn foo(_: *const i32) void {}
51785192 {#code_end#}
51795193 <p>
51805194 In addition, pointers coerce to const optional pointers:
......@@ -5424,7 +5438,7 @@ test "coercion between unions and enums" {
54245438test "coercion of zero bit types" {
54255439 var x: void = {};
54265440 var y: *void = x;
5427 //var z: void = y; // TODO
5441 _ = y;
54285442}
54295443 {#code_end#}
54305444 {#header_close#}
......@@ -6569,6 +6583,7 @@ var x: i32 = 1;
65696583test "suspend with no resume" {
65706584 var frame = async func();
65716585 try expect(x == 2);
6586 _ = frame;
65726587}
65736588
65746589fn func() void {
......@@ -6800,6 +6815,7 @@ fn amain() !void {
68006815
68016816var global_download_frame: anyframe = undefined;
68026817fn fetchUrl(allocator: *Allocator, url: []const u8) ![]u8 {
6818 _ = url; // this is just an example, we don't actually do it!
68036819 const result = try std.mem.dupe(allocator, u8, "this is the downloaded url contents");
68046820 errdefer allocator.free(result);
68056821 suspend {
......@@ -6811,6 +6827,7 @@ fn fetchUrl(allocator: *Allocator, url: []const u8) ![]u8 {
68116827
68126828var global_file_frame: anyframe = undefined;
68136829fn readFile(allocator: *Allocator, filename: []const u8) ![]u8 {
6830 _ = filename; // this is just an example, we don't actually do it!
68146831 const result = try std.mem.dupe(allocator, u8, "this is the file contents");
68156832 errdefer allocator.free(result);
68166833 suspend {
......@@ -6869,6 +6886,7 @@ fn amain() !void {
68696886}
68706887
68716888fn fetchUrl(allocator: *Allocator, url: []const u8) ![]u8 {
6889 _ = url; // this is just an example, we don't actually do it!
68726890 const result = try std.mem.dupe(allocator, u8, "this is the downloaded url contents");
68736891 errdefer allocator.free(result);
68746892 std.debug.print("fetchUrl returning\n", .{});
......@@ -6876,6 +6894,7 @@ fn fetchUrl(allocator: *Allocator, url: []const u8) ![]u8 {
68766894}
68776895
68786896fn readFile(allocator: *Allocator, filename: []const u8) ![]u8 {
6897 _ = filename; // this is just an example, we don't actually do it!
68796898 const result = try std.mem.dupe(allocator, u8, "this is the file contents");
68806899 errdefer allocator.free(result);
68816900 std.debug.print("readFile returning\n", .{});
......@@ -8584,6 +8603,7 @@ fn List(comptime T: type) type {
85848603test "integer cast panic" {
85858604 var a: u16 = 0xabcd;
85868605 var b: u8 = @intCast(u8, a);
8606 _ = b;
85878607}
85888608 {#code_end#}
85898609 <p>
......@@ -8839,6 +8859,7 @@ comptime {
88398859 {#code_begin|exe_err#}
88408860pub fn main() void {
88418861 var x = foo("hello");
8862 _ = x;
88428863}
88438864
88448865fn foo(x: []const u8) u8 {
......@@ -9107,6 +9128,7 @@ pub fn main() void {
91079128comptime {
91089129 const optional_number: ?i32 = null;
91099130 const number = optional_number.?;
9131 _ = number;
91109132}
91119133 {#code_end#}
91129134 <p>At runtime:</p>
......@@ -9140,6 +9162,7 @@ pub fn main() void {
91409162 {#code_begin|test_err|caught unexpected error 'UnableToReturnNumber'#}
91419163comptime {
91429164 const number = getNumberOrFail() catch unreachable;
9165 _ = number;
91439166}
91449167
91459168fn getNumberOrFail() !i32 {
......@@ -9187,6 +9210,7 @@ comptime {
91879210 const err = error.AnError;
91889211 const number = @errorToInt(err) + 10;
91899212 const invalid_err = @intToError(number);
9213 _ = invalid_err;
91909214}
91919215 {#code_end#}
91929216 <p>At runtime:</p>
......@@ -9197,7 +9221,7 @@ pub fn main() void {
91979221 var err = error.AnError;
91989222 var number = @errorToInt(err) + 500;
91999223 var invalid_err = @intToError(number);
9200 std.debug.print("value: {}\n", .{number});
9224 std.debug.print("value: {}\n", .{invalid_err});
92019225}
92029226 {#code_end#}
92039227 {#header_close#}
......@@ -9212,6 +9236,7 @@ const Foo = enum {
92129236comptime {
92139237 const a: u2 = 3;
92149238 const b = @intToEnum(Foo, a);
9239 _ = b;
92159240}
92169241 {#code_end#}
92179242 <p>At runtime:</p>
......@@ -9396,6 +9421,7 @@ comptime {
93969421pub fn main() void {
93979422 var opt_ptr: ?*i32 = null;
93989423 var ptr = @ptrCast(*i32, opt_ptr);
9424 _ = ptr;
93999425}
94009426 {#code_end#}
94019427 {#header_close#}
......@@ -9523,7 +9549,9 @@ pub fn main() !void {
95239549 This is why it is an error to pass a string literal to a mutable slice, like this:
95249550 </p>
95259551 {#code_begin|test_err|expected type '[]u8'#}
9526fn foo(s: []u8) void {}
9552fn foo(s: []u8) void {
9553 _ = s;
9554}
95279555
95289556test "string literal to mutable slice" {
95299557 foo("hello");
......@@ -9531,7 +9559,9 @@ test "string literal to mutable slice" {
95319559 {#code_end#}
95329560 <p>However if you make the slice constant, then it works:</p>
95339561 {#code_begin|test|strlit#}
9534fn foo(s: []const u8) void {}
9562fn foo(s: []const u8) void {
9563 _ = s;
9564}
95359565
95369566test "string literal to constant slice" {
95379567 foo("hello");
......@@ -10476,7 +10506,7 @@ coding style.
1047610506 </p>
1047710507 {#header_close#}
1047810508 {#header_open|Examples#}
10479 {#code_begin|syntax#}
10509 <pre>{#syntax#}
1048010510const namespace_name = @import("dir_name/file_name.zig");
1048110511const TypeName = @import("dir_name/TypeName.zig");
1048210512var global_var: i32 = undefined;
......@@ -10520,7 +10550,7 @@ const XmlParser = struct {
1052010550
1052110551// The initials BE (Big Endian) are just another word in Zig identifier names.
1052210552fn readU32Be() u32 {}
10523 {#code_end#}
10553 {#endsyntax#}</pre>
1052410554 <p>
1052510555 See the Zig Standard Library for more examples.
1052610556 </p>