diff --git a/lib/std/json/test.zig b/lib/std/json/test.zig index b6ff03350c..b0d873c910 100644 --- a/lib/std/json/test.zig +++ b/lib/std/json/test.zig @@ -29,8 +29,7 @@ fn ok(s: []const u8) !void { fn err(s: []const u8) void { testing.expect(!json.validate(s)); - testNonStreaming(s) catch return; - testing.expect(false); + testing.expect(std.meta.isError(testNonStreaming(s))); } fn utf8Error(s: []const u8) void { @@ -48,8 +47,7 @@ fn any(s: []const u8) void { fn anyStreamingErrNonStreaming(s: []const u8) void { _ = json.validate(s); - testNonStreaming(s) catch return; - testing.expect(false); + testing.expect(std.meta.isError(testNonStreaming(s))); } fn roundTrip(s: []const u8) !void { diff --git a/lib/std/meta.zig b/lib/std/meta.zig index c6f800ca2c..dd6f500076 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -1346,3 +1346,13 @@ test "shuffleVectorIndex" { testing.expect(shuffleVectorIndex(6, vector_len) == -3); testing.expect(shuffleVectorIndex(7, vector_len) == -4); } + +/// Returns whether `error_union` contains an error. +pub fn isError(error_union: anytype) bool { + return if (error_union) |_| false else |_| true; +} + +test "isError" { + std.testing.expect(isError(math.absInt(@as(i8, -128)))); + std.testing.expect(!isError(math.absInt(@as(i8, -127)))); +} diff --git a/lib/std/unicode.zig b/lib/std/unicode.zig index f9ad6e3eb5..9eed0f466f 100644 --- a/lib/std/unicode.zig +++ b/lib/std/unicode.zig @@ -206,7 +206,7 @@ pub fn utf8ValidateSlice(s: []const u8) bool { return false; } - if (utf8Decode(s[i .. i + cp_len])) |_| {} else |_| { + if (std.meta.isError(utf8Decode(s[i .. i + cp_len]))) { return false; } i += cp_len; diff --git a/src/translate_c.zig b/src/translate_c.zig index 60b3961e6e..d44bbd39c4 100644 --- a/src/translate_c.zig +++ b/src/translate_c.zig @@ -8,6 +8,7 @@ const ctok = std.c.tokenizer; const CToken = std.c.Token; const mem = std.mem; const math = std.math; +const meta = std.meta; const ast = @import("translate_c/ast.zig"); const Node = ast.Node; const Tag = Node.Tag; @@ -1741,7 +1742,7 @@ fn transImplicitCastExpr( } fn isBuiltinDefined(name: []const u8) bool { - inline for (std.meta.declarations(c_builtins)) |decl| { + inline for (meta.declarations(c_builtins)) |decl| { if (std.mem.eql(u8, name, decl.name)) return true; } return false; @@ -3157,7 +3158,7 @@ const ClangFunctionType = union(enum) { NoProto: *const clang.FunctionType, fn getReturnType(self: @This()) clang.QualType { - switch (@as(std.meta.Tag(@This()), self)) { + switch (@as(meta.Tag(@This()), self)) { .Proto => return self.Proto.getReturnType(), .NoProto => return self.NoProto.getReturnType(), } @@ -4106,7 +4107,7 @@ fn transCreateNodeAPInt(c: *Context, int: *const clang.APSInt) !Node { } fn transCreateNodeNumber(c: *Context, num: anytype, num_kind: enum { int, float }) !Node { - const fmt_s = if (comptime std.meta.trait.isNumber(@TypeOf(num))) "{d}" else "{s}"; + const fmt_s = if (comptime meta.trait.isNumber(@TypeOf(num))) "{d}" else "{s}"; const str = try std.fmt.allocPrint(c.arena, fmt_s, .{num}); if (num_kind == .float) return Tag.float_literal.create(c.arena, str) @@ -4861,12 +4862,12 @@ fn parseCNumLit(c: *Context, m: *MacroCtx) ParseError!Node { // make the output less noisy by skipping promoteIntLiteral where // it's guaranteed to not be required because of C standard type constraints const guaranteed_to_fit = switch (suffix) { - .none => if (math.cast(i16, value)) |_| true else |_| false, - .u => if (math.cast(u16, value)) |_| true else |_| false, - .l => if (math.cast(i32, value)) |_| true else |_| false, - .lu => if (math.cast(u32, value)) |_| true else |_| false, - .ll => if (math.cast(i64, value)) |_| true else |_| false, - .llu => if (math.cast(u64, value)) |_| true else |_| false, + .none => !meta.isError(math.cast(i16, value)), + .u => !meta.isError(math.cast(u16, value)), + .l => !meta.isError(math.cast(i32, value)), + .lu => !meta.isError(math.cast(u32, value)), + .ll => !meta.isError(math.cast(i64, value)), + .llu => !meta.isError(math.cast(u64, value)), .f => unreachable, };