mozilla / rust-code-analysis

Library to analyze and collect metrics on source code
https://mozilla.github.io/rust-code-analysis/
289 stars 49 forks source link

Fix handling of `?` operator in Rust `nexits` metric #1123

Closed alexle0nte closed 2 months ago

alexle0nte commented 2 months ago

As pointed out by #1113, the ? operator can also represent an exit point within a Rust function, and therefore, it must be counted in the nexits metric.

However, not all Rust::QMARK nodes should be counted as exit points. For example, in this code:

pub fn assert_ser_tokens<T: ?Sized>(value: &T, tokens: &[Token])
where
    T: Serialize,
{
    let mut ser = Serializer::new(tokens);
    match value.serialize(&mut ser) {
        Ok(_) => {}
        Err(err) => panic!("value failed to serialize: {}", err),
    }

    if ser.remaining() > 0 {
        panic!("{} remaining tokens", ser.remaining());
    }
}

the ? on the first line, inside <T: ?Sized>, doesn't represent an exit point.