@@ -965,7 +965,7 @@ test "thread local storage" {
965
965
thread2.wait();
966
966
}
967
967
968
- fn testTls(context : void) void {
968
+ fn testTls(_ : void) void {
969
969
assert(x == 1234);
970
970
x += 1;
971
971
assert(x == 1235);
@@ -2502,6 +2502,8 @@ test "struct namespaced variable" {
2502
2502
2503
2503
// you can still instantiate an empty struct
2504
2504
const does_nothing = Empty {};
2505
+
2506
+ _ = does_nothing;
2505
2507
}
2506
2508
2507
2509
// struct field order is determined by the compiler for optimal performance.
@@ -3026,11 +3028,12 @@ const Foo = enum { a, b, c };
3026
3028
export fn entry(foo: Foo) void { }
3027
3029
{#code_end#}
3028
3030
<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:
3030
3033
</p>
3031
3034
{#code_begin|obj#}
3032
- const Foo = extern enum { a, b, c };
3033
- export fn entry(foo: Foo) void { }
3035
+ const Foo = enum(c_int) { a, b, c };
3036
+ export fn entry(foo: Foo) void { _ = foo; }
3034
3037
{#code_end#}
3035
3038
{#header_close#}
3036
3039
@@ -3392,9 +3395,11 @@ test "inside test block" {
3392
3395
test "separate scopes" {
3393
3396
{
3394
3397
const pi = 3.14;
3398
+ _ = pi;
3395
3399
}
3396
3400
{
3397
3401
var pi: bool = true;
3402
+ _ = pi;
3398
3403
}
3399
3404
}
3400
3405
{#code_end#}
@@ -3432,7 +3437,7 @@ test "switch simple" {
3432
3437
// Switching on arbitrary expressions is allowed as long as the
3433
3438
// expression is known at compile-time.
3434
3439
zz => zz,
3435
- comptime blk: {
3440
+ blk: {
3436
3441
const d: u32 = 5;
3437
3442
const e: u32 = 100;
3438
3443
break :blk d + e;
@@ -3831,7 +3836,7 @@ test "for basics" {
3831
3836
// To access the index of iteration, specify a second capture value.
3832
3837
// This is zero-indexed.
3833
3838
var sum2: i32 = 0;
3834
- for (items) |value , i| {
3839
+ for (items) |_ , i| {
3835
3840
try expect(@TypeOf(i) == usize);
3836
3841
sum2 += @intCast(i32, i);
3837
3842
}
@@ -3984,7 +3989,7 @@ test "if optional" {
3984
3989
}
3985
3990
3986
3991
const b: ?u32 = null;
3987
- if (b) |value | {
3992
+ if (b) |_ | {
3988
3993
unreachable;
3989
3994
} else {
3990
3995
try expect(true);
@@ -4021,11 +4026,13 @@ test "if error union" {
4021
4026
if (a) |value| {
4022
4027
try expect(value == 0);
4023
4028
} else |err| {
4029
+ _ = err;
4024
4030
unreachable;
4025
4031
}
4026
4032
4027
4033
const b: anyerror!u32 = error.BadValue;
4028
4034
if (b) |value| {
4035
+ _ = value;
4029
4036
unreachable;
4030
4037
} else |err| {
4031
4038
try expect(err == error.BadValue);
@@ -4045,13 +4052,13 @@ test "if error union" {
4045
4052
var c: anyerror!u32 = 3;
4046
4053
if (c) |*value| {
4047
4054
value.* = 9;
4048
- } else |err | {
4055
+ } else |_ | {
4049
4056
unreachable;
4050
4057
}
4051
4058
4052
4059
if (c) |value| {
4053
4060
try expect(value == 9);
4054
- } else |err | {
4061
+ } else |_ | {
4055
4062
unreachable;
4056
4063
}
4057
4064
}
@@ -4064,18 +4071,20 @@ test "if error union with optional" {
4064
4071
if (a) |optional_value| {
4065
4072
try expect(optional_value.? == 0);
4066
4073
} else |err| {
4074
+ _ = err;
4067
4075
unreachable;
4068
4076
}
4069
4077
4070
4078
const b: anyerror!?u32 = null;
4071
4079
if (b) |optional_value| {
4072
4080
try expect(optional_value == null);
4073
- } else |err | {
4081
+ } else |_ | {
4074
4082
unreachable;
4075
4083
}
4076
4084
4077
4085
const c: anyerror!?u32 = error.BadValue;
4078
4086
if (c) |optional_value| {
4087
+ _ = optional_value;
4079
4088
unreachable;
4080
4089
} else |err| {
4081
4090
try expect(err == error.BadValue);
@@ -4087,13 +4096,13 @@ test "if error union with optional" {
4087
4096
if (optional_value.*) |*value| {
4088
4097
value.* = 9;
4089
4098
}
4090
- } else |err | {
4099
+ } else |_ | {
4091
4100
unreachable;
4092
4101
}
4093
4102
4094
4103
if (d) |optional_value| {
4095
4104
try expect(optional_value.? == 9);
4096
- } else |err | {
4105
+ } else |_ | {
4097
4106
unreachable;
4098
4107
}
4099
4108
}
@@ -4246,6 +4255,7 @@ test "type of unreachable" {
4246
4255
{#code_begin|test#}
4247
4256
fn foo(condition: bool, b: u32) void {
4248
4257
const a = if (condition) b else return;
4258
+ _ = a;
4249
4259
@panic("do something with a");
4250
4260
}
4251
4261
test "noreturn" {
@@ -4574,7 +4584,7 @@ test "parse u64" {
4574
4584
{#code_begin|syntax#}
4575
4585
fn doAThing(str: []u8) void {
4576
4586
const number = parseU64(str, 10) catch 13;
4577
- // ...
4587
+ _ = number; // ...
4578
4588
}
4579
4589
{#code_end#}
4580
4590
<p>
@@ -4589,7 +4599,7 @@ fn doAThing(str: []u8) void {
4589
4599
{#code_begin|syntax#}
4590
4600
fn doAThing(str: []u8) !void {
4591
4601
const number = parseU64(str, 10) catch |err| return err;
4592
- // ...
4602
+ _ = number; // ...
4593
4603
}
4594
4604
{#code_end#}
4595
4605
<p>
@@ -4598,7 +4608,7 @@ fn doAThing(str: []u8) !void {
4598
4608
{#code_begin|syntax#}
4599
4609
fn doAThing(str: []u8) !void {
4600
4610
const number = try parseU64(str, 10);
4601
- // ...
4611
+ _ = number; // ...
4602
4612
}
4603
4613
{#code_end#}
4604
4614
<p>
@@ -5022,7 +5032,7 @@ extern fn malloc(size: size_t) ?*u8;
5022
5032
5023
5033
fn doAThing() ?*Foo {
5024
5034
const ptr = malloc(1234) orelse return null;
5025
- // ...
5035
+ _ = ptr; // ...
5026
5036
}
5027
5037
{#code_end#}
5028
5038
<p>
@@ -5135,18 +5145,22 @@ test "optional pointers" {
5135
5145
test "type coercion - variable declaration" {
5136
5146
var a: u8 = 1;
5137
5147
var b: u16 = a;
5148
+ _ = b;
5138
5149
}
5139
5150
5140
5151
test "type coercion - function call" {
5141
5152
var a: u8 = 1;
5142
5153
foo(a);
5143
5154
}
5144
5155
5145
- fn foo(b: u16) void {}
5156
+ fn foo(b: u16) void {
5157
+ _ = b;
5158
+ }
5146
5159
5147
5160
test "type coercion - @as builtin" {
5148
5161
var a: u8 = 1;
5149
5162
var b = @as(u16, a);
5163
+ _ = b;
5150
5164
}
5151
5165
{#code_end#}
5152
5166
<p>
@@ -5174,7 +5188,7 @@ test "type coercion - const qualification" {
5174
5188
foo(b);
5175
5189
}
5176
5190
5177
- fn foo(a : *const i32) void {}
5191
+ fn foo(_ : *const i32) void {}
5178
5192
{#code_end#}
5179
5193
<p>
5180
5194
In addition, pointers coerce to const optional pointers:
@@ -5424,7 +5438,7 @@ test "coercion between unions and enums" {
5424
5438
test "coercion of zero bit types" {
5425
5439
var x: void = {};
5426
5440
var y: *void = x;
5427
- //var z: void = y; // TODO
5441
+ _ = y;
5428
5442
}
5429
5443
{#code_end#}
5430
5444
{#header_close#}
@@ -6569,6 +6583,7 @@ var x: i32 = 1;
6569
6583
test "suspend with no resume" {
6570
6584
var frame = async func();
6571
6585
try expect(x == 2);
6586
+ _ = frame;
6572
6587
}
6573
6588
6574
6589
fn func() void {
@@ -6800,6 +6815,7 @@ fn amain() !void {
6800
6815
6801
6816
var global_download_frame: anyframe = undefined;
6802
6817
fn fetchUrl(allocator: *Allocator, url: []const u8) ![]u8 {
6818
+ _ = url; // this is just an example, we don't actually do it!
6803
6819
const result = try std.mem.dupe(allocator, u8, "this is the downloaded url contents");
6804
6820
errdefer allocator.free(result);
6805
6821
suspend {
@@ -6811,6 +6827,7 @@ fn fetchUrl(allocator: *Allocator, url: []const u8) ![]u8 {
6811
6827
6812
6828
var global_file_frame: anyframe = undefined;
6813
6829
fn readFile(allocator: *Allocator, filename: []const u8) ![]u8 {
6830
+ _ = filename; // this is just an example, we don't actually do it!
6814
6831
const result = try std.mem.dupe(allocator, u8, "this is the file contents");
6815
6832
errdefer allocator.free(result);
6816
6833
suspend {
@@ -6869,13 +6886,15 @@ fn amain() !void {
6869
6886
}
6870
6887
6871
6888
fn fetchUrl(allocator: *Allocator, url: []const u8) ![]u8 {
6889
+ _ = url; // this is just an example, we don't actually do it!
6872
6890
const result = try std.mem.dupe(allocator, u8, "this is the downloaded url contents");
6873
6891
errdefer allocator.free(result);
6874
6892
std.debug.print("fetchUrl returning\n", .{});
6875
6893
return result;
6876
6894
}
6877
6895
6878
6896
fn readFile(allocator: *Allocator, filename: []const u8) ![]u8 {
6897
+ _ = filename; // this is just an example, we don't actually do it!
6879
6898
const result = try std.mem.dupe(allocator, u8, "this is the file contents");
6880
6899
errdefer allocator.free(result);
6881
6900
std.debug.print("readFile returning\n", .{});
@@ -8584,6 +8603,7 @@ fn List(comptime T: type) type {
8584
8603
test "integer cast panic" {
8585
8604
var a: u16 = 0xabcd;
8586
8605
var b: u8 = @intCast(u8, a);
8606
+ _ = b;
8587
8607
}
8588
8608
{#code_end#}
8589
8609
<p>
@@ -8839,6 +8859,7 @@ comptime {
8839
8859
{#code_begin|exe_err#}
8840
8860
pub fn main() void {
8841
8861
var x = foo("hello");
8862
+ _ = x;
8842
8863
}
8843
8864
8844
8865
fn foo(x: []const u8) u8 {
@@ -9107,6 +9128,7 @@ pub fn main() void {
9107
9128
comptime {
9108
9129
const optional_number: ?i32 = null;
9109
9130
const number = optional_number.?;
9131
+ _ = number;
9110
9132
}
9111
9133
{#code_end#}
9112
9134
<p>At runtime:</p>
@@ -9140,6 +9162,7 @@ pub fn main() void {
9140
9162
{#code_begin|test_err|caught unexpected error 'UnableToReturnNumber'#}
9141
9163
comptime {
9142
9164
const number = getNumberOrFail() catch unreachable;
9165
+ _ = number;
9143
9166
}
9144
9167
9145
9168
fn getNumberOrFail() !i32 {
@@ -9187,6 +9210,7 @@ comptime {
9187
9210
const err = error.AnError;
9188
9211
const number = @errorToInt(err) + 10;
9189
9212
const invalid_err = @intToError(number);
9213
+ _ = invalid_err;
9190
9214
}
9191
9215
{#code_end#}
9192
9216
<p>At runtime:</p>
@@ -9197,7 +9221,7 @@ pub fn main() void {
9197
9221
var err = error.AnError;
9198
9222
var number = @errorToInt(err) + 500;
9199
9223
var invalid_err = @intToError(number);
9200
- std.debug.print("value: {}\n", .{number });
9224
+ std.debug.print("value: {}\n", .{invalid_err });
9201
9225
}
9202
9226
{#code_end#}
9203
9227
{#header_close#}
@@ -9212,6 +9236,7 @@ const Foo = enum {
9212
9236
comptime {
9213
9237
const a: u2 = 3;
9214
9238
const b = @intToEnum(Foo, a);
9239
+ _ = b;
9215
9240
}
9216
9241
{#code_end#}
9217
9242
<p>At runtime:</p>
@@ -9396,6 +9421,7 @@ comptime {
9396
9421
pub fn main() void {
9397
9422
var opt_ptr: ?*i32 = null;
9398
9423
var ptr = @ptrCast(*i32, opt_ptr);
9424
+ _ = ptr;
9399
9425
}
9400
9426
{#code_end#}
9401
9427
{#header_close#}
@@ -9523,15 +9549,19 @@ pub fn main() !void {
9523
9549
This is why it is an error to pass a string literal to a mutable slice, like this:
9524
9550
</p>
9525
9551
{#code_begin|test_err|expected type '[]u8'#}
9526
- fn foo(s: []u8) void {}
9552
+ fn foo(s: []u8) void {
9553
+ _ = s;
9554
+ }
9527
9555
9528
9556
test "string literal to mutable slice" {
9529
9557
foo("hello");
9530
9558
}
9531
9559
{#code_end#}
9532
9560
<p>However if you make the slice constant, then it works:</p>
9533
9561
{#code_begin|test|strlit#}
9534
- fn foo(s: []const u8) void {}
9562
+ fn foo(s: []const u8) void {
9563
+ _ = s;
9564
+ }
9535
9565
9536
9566
test "string literal to constant slice" {
9537
9567
foo("hello");
@@ -10476,7 +10506,7 @@ coding style.
10476
10506
</p>
10477
10507
{#header_close#}
10478
10508
{#header_open|Examples#}
10479
- {#code_begin| syntax#}
10509
+ <pre>{# syntax#}
10480
10510
const namespace_name = @import("dir_name/file_name.zig");
10481
10511
const TypeName = @import("dir_name/TypeName.zig");
10482
10512
var global_var: i32 = undefined;
@@ -10520,7 +10550,7 @@ const XmlParser = struct {
10520
10550
10521
10551
// The initials BE (Big Endian) are just another word in Zig identifier names.
10522
10552
fn readU32Be() u32 {}
10523
- {#code_end#}
10553
+ {#endsyntax#}</pre>
10524
10554
<p>
10525
10555
See the Zig Standard Library for more examples.
10526
10556
</p>
0 commit comments