From 32595320807fdb8a175674621b0b2216527f9d4f Mon Sep 17 00:00:00 2001
From: Andrew Kelley
- For a C-ABI-compatible enum, use {#syntax#}extern enum{#endsyntax#}:
+ For a C-ABI-compatible enum, provide an explicit tag type to
+ the enum:
@@ -4589,7 +4599,7 @@ fn doAThing(str: []u8) void {
{#code_begin|syntax#}
fn doAThing(str: []u8) !void {
const number = parseU64(str, 10) catch |err| return err;
- // ...
+ _ = number; // ...
}
{#code_end#}
@@ -4598,7 +4608,7 @@ fn doAThing(str: []u8) !void {
{#code_begin|syntax#}
fn doAThing(str: []u8) !void {
const number = try parseU64(str, 10);
- // ...
+ _ = number; // ...
}
{#code_end#}
@@ -5022,7 +5032,7 @@ extern fn malloc(size: size_t) ?*u8;
fn doAThing() ?*Foo {
const ptr = malloc(1234) orelse return null;
- // ...
+ _ = ptr; // ...
}
{#code_end#}
@@ -5135,6 +5145,7 @@ test "optional pointers" {
test "type coercion - variable declaration" {
var a: u8 = 1;
var b: u16 = a;
+ _ = b;
}
test "type coercion - function call" {
@@ -5142,11 +5153,14 @@ test "type coercion - function call" {
foo(a);
}
-fn foo(b: u16) void {}
+fn foo(b: u16) void {
+ _ = b;
+}
test "type coercion - @as builtin" {
var a: u8 = 1;
var b = @as(u16, a);
+ _ = b;
}
{#code_end#}
@@ -5174,7 +5188,7 @@ test "type coercion - const qualification" {
foo(b);
}
-fn foo(a: *const i32) void {}
+fn foo(_: *const i32) void {}
{#code_end#}
In addition, pointers coerce to const optional pointers:
@@ -5424,7 +5438,7 @@ test "coercion between unions and enums" {
test "coercion of zero bit types" {
var x: void = {};
var y: *void = x;
- //var z: void = y; // TODO
+ _ = y;
}
{#code_end#}
{#header_close#}
@@ -6569,6 +6583,7 @@ var x: i32 = 1;
test "suspend with no resume" {
var frame = async func();
try expect(x == 2);
+ _ = frame;
}
fn func() void {
@@ -6800,6 +6815,7 @@ fn amain() !void {
var global_download_frame: anyframe = undefined;
fn fetchUrl(allocator: *Allocator, url: []const u8) ![]u8 {
+ _ = url; // this is just an example, we don't actually do it!
const result = try std.mem.dupe(allocator, u8, "this is the downloaded url contents");
errdefer allocator.free(result);
suspend {
@@ -6811,6 +6827,7 @@ fn fetchUrl(allocator: *Allocator, url: []const u8) ![]u8 {
var global_file_frame: anyframe = undefined;
fn readFile(allocator: *Allocator, filename: []const u8) ![]u8 {
+ _ = filename; // this is just an example, we don't actually do it!
const result = try std.mem.dupe(allocator, u8, "this is the file contents");
errdefer allocator.free(result);
suspend {
@@ -6869,6 +6886,7 @@ fn amain() !void {
}
fn fetchUrl(allocator: *Allocator, url: []const u8) ![]u8 {
+ _ = url; // this is just an example, we don't actually do it!
const result = try std.mem.dupe(allocator, u8, "this is the downloaded url contents");
errdefer allocator.free(result);
std.debug.print("fetchUrl returning\n", .{});
@@ -6876,6 +6894,7 @@ fn fetchUrl(allocator: *Allocator, url: []const u8) ![]u8 {
}
fn readFile(allocator: *Allocator, filename: []const u8) ![]u8 {
+ _ = filename; // this is just an example, we don't actually do it!
const result = try std.mem.dupe(allocator, u8, "this is the file contents");
errdefer allocator.free(result);
std.debug.print("readFile returning\n", .{});
@@ -8584,6 +8603,7 @@ fn List(comptime T: type) type {
test "integer cast panic" {
var a: u16 = 0xabcd;
var b: u8 = @intCast(u8, a);
+ _ = b;
}
{#code_end#}
@@ -8839,6 +8859,7 @@ comptime {
{#code_begin|exe_err#}
pub fn main() void {
var x = foo("hello");
+ _ = x;
}
fn foo(x: []const u8) u8 {
@@ -9107,6 +9128,7 @@ pub fn main() void {
comptime {
const optional_number: ?i32 = null;
const number = optional_number.?;
+ _ = number;
}
{#code_end#}
At runtime: At runtime: At runtime:
However if you make the slice constant, then it works:
{#code_begin|test|strlit#} -fn foo(s: []const u8) void {} +fn foo(s: []const u8) void { + _ = s; +} test "string literal to constant slice" { foo("hello"); @@ -10476,7 +10506,7 @@ coding style. {#header_close#} {#header_open|Examples#} - {#code_begin|syntax#} +{#syntax#}
const namespace_name = @import("dir_name/file_name.zig");
const TypeName = @import("dir_name/TypeName.zig");
var global_var: i32 = undefined;
@@ -10520,7 +10550,7 @@ const XmlParser = struct {
// The initials BE (Big Endian) are just another word in Zig identifier names.
fn readU32Be() u32 {}
- {#code_end#}
+ {#endsyntax#}
See the Zig Standard Library for more examples.