ziglint Changelog View on GitHub

Z036: Use specific std.testing assertions

std.testing.expect reports only error.TestUnexpectedResult on failure. When its argument is a standard comparison function, std.testing has a dedicated assertion that prints both values and highlights the difference.

Flagged patternReplacement
expect(std.mem.startsWith(u8, actual, expected))expectStringStartsWith(actual, expected)
expect(std.mem.endsWith(u8, actual, expected))expectStringEndsWith(actual, expected)
expect(std.mem.eql(u8, a, b))expectEqualStrings(expected, actual)
expect(std.mem.eql(T, a, b))expectEqualSlices(T, expected, actual)
expect(std.meta.eql(a, b))expectEqual(expected, actual)
expect(std.math.approxEqAbs(T, a, b, tol))expectApproxEqAbs(expected, actual, tolerance)
expect(std.math.approxEqRel(T, a, b, tol))expectApproxEqRel(expected, actual, tolerance)

Check the argument order when rewriting:

Examples

Bad

const std = @import("std");

test "strings" {
    const actual: []const u8 = "hello world";
    // expect: Z036
    try std.testing.expect(std.mem.startsWith(u8, actual, "hello"));
    // expect: Z036
    try std.testing.expect(std.mem.endsWith(u8, actual, "world"));
    // expect: Z036
    try std.testing.expect(std.mem.eql(u8, actual, "hello world"));
}

Slices, structs, and floats have dedicated assertions too:

const std = @import("std");

test "other comparisons" {
    const nums = [_]u32{ 1, 2, 3 };
    // expect: Z036
    try std.testing.expect(std.mem.eql(u32, &nums, &.{ 1, 2, 3 }));
    // expect: Z036
    try std.testing.expect(std.meta.eql(nums, [_]u32{ 1, 2, 3 }));
    const actual: f64 = 1.0;
    // expect: Z036
    try std.testing.expect(std.math.approxEqAbs(f64, actual, 1.0, 1e-6));
    // expect: Z036
    try std.testing.expect(std.math.approxEqRel(f64, actual, 1.0, 1e-6));
}

Good

const std = @import("std");

test "strings" {
    const actual: []const u8 = "hello world";
    try std.testing.expectStringStartsWith(actual, "hello");
    try std.testing.expectStringEndsWith(actual, "world");
    try std.testing.expectEqualStrings("hello world", actual);
}
const std = @import("std");

test "other comparisons" {
    const nums = [_]u32{ 1, 2, 3 };
    try std.testing.expectEqualSlices(u32, &.{ 1, 2, 3 }, &nums);
    try std.testing.expectEqual([_]u32{ 1, 2, 3 }, nums);
    const actual: f64 = 1.0;
    try std.testing.expectApproxEqAbs(1.0, actual, 1e-6);
    try std.testing.expectApproxEqRel(1.0, actual, 1e-6);
}

Negated checks and non-byte prefix or suffix checks have no dedicated std.testing helper, so they are allowed:

const std = @import("std");

test "allowed expect calls" {
    const actual: []const u8 = "hello world";
    try std.testing.expect(!std.mem.startsWith(u8, actual, "bye"));
    try std.testing.expect(!std.mem.eql(u8, actual, "bye"));
    const nums = [_]u32{ 1, 2, 3 };
    try std.testing.expect(std.mem.startsWith(u32, &nums, &.{ 1, 2 }));
    try std.testing.expect(std.mem.endsWith(u32, &nums, &.{ 2, 3 }));
}

Limitations

The check only resolves top-level aliases. An alias declared inside a function or a test block is not followed, so calls through a local alias are missed.

The check does not track local shadowing of top-level aliases. With const mem = std.mem; at file scope, a local const mem that also defines an eql function is matched as std.mem.eql and reported with a replacement that will not compile.

Type arguments are matched by identifier name. A type alias for u8 is treated as a non-u8 slice type rather than a string.

Rationale

When std.testing.expect(std.mem.eql(u8, actual, "expected")) fails, the test runner prints only error.TestUnexpectedResult and a stack trace. std.testing.expectEqualStrings("expected", actual) prints both strings with visible newlines and points to the first differing line and byte. The other specific assertions print comparable diagnostics for slices, values, and floating-point tolerances.