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