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

rust: a question mark operator is a conditional exit #1113

Closed samueltardieu closed 2 months ago

samueltardieu commented 2 months ago

A question mark operator ? may exit the current function the same way a return will. It must be counted in the "nexits" statistics.

samueltardieu commented 2 months ago

Is there something missing for this PR to be reviewed and merged?

alexle0nte commented 2 months ago

While working on #1091, I noticed that some tests are failing because all nodes corresponding to a Rust::QMARK are mistakenly 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>, should not be counted as an exit point. Therefore, adding Rust::QMARK in an OR condition with Rust::ReturnExpression doesn't fully address the issue.

calixteman commented 2 months ago

@alexle0nte could you file a bug please ? @samueltardieu would you mind to have a look on it ?

Thank you.