Surprising(?) unused variable

Why does clippy think b is unused, if the code does this:

    assert!(matches!(n, Some(b)));

Complete example here:

Because it is unused.

A good first debugging step is to try to falsify your own assumption:

use bytes::Bytes;

fn main() {
    let n = Some(Bytes::from("1234"));
    let b = Bytes::from("xxxx");
    assert!(matches!(n, Some(b)));
}

the first argument to matches!() is the scrutinee, the second is the pattern.

if you click "tools" => "expand macros" in the playground, you can see what the expanded code looks like.

use bytes::Bytes;

fn main() {
    let n = Some(Bytes::from("1234"));
    let b = Bytes::from("1234");
    assert!(matches!(n, Some(bytes) if bytes == b));
}

Explanation Some(thing) in a pattern match (inside matches!() or in a match on the left of the =>) binds thing to the value found.

you probably want to use the == operator, or use assert_eq!() instead of matches!().

Yeah, expansion made it obvious -- thank you.

In nightly you can use assert_matches in std - Rust

This works the same way as assert!(matches!(...)) which was incorrect, so it won't help here.