Skip to content

Commit

Permalink
rust: a question mark operator is a conditional exit (#1113)
Browse files Browse the repository at this point in the history
A question mark operator `?` may exit the current function the same way a `return`
will. It must be counted in the "nexits" statistics.
  • Loading branch information
samueltardieu authored Sep 13, 2024
1 parent e0a5c03 commit b035001
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/metrics/exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl Exit for TsxCode {

impl Exit for RustCode {
fn compute(node: &Node, stats: &mut Stats) {
if matches!(node.kind_id().into(), Rust::ReturnExpression)
if matches!(node.kind_id().into(), Rust::ReturnExpression | Rust::QMARK)
|| Self::is_func(node) && node.child_by_field_name("return_type").is_some()
{
stats.exit += 1;
Expand Down Expand Up @@ -222,6 +222,23 @@ mod tests {
});
}

#[test]
fn rust_question_mark() {
check_metrics::<RustParser>("let _ = a? + b? + c?;", "foo.rs", |metric| {
// 0 functions
insta::assert_json_snapshot!(
metric.nexits,
@r###"
{
"sum": 3.0,
"average": null,
"min": 3.0,
"max": 3.0
}"###
);
});
}

#[test]
fn c_no_exit() {
check_metrics::<CppParser>("int a = 42;", "foo.c", |metric| {
Expand Down

0 comments on commit b035001

Please sign in to comment.