From 175122d3023b413b33fc65509aa6a76484154436 Mon Sep 17 00:00:00 2001 From: Samuel Tardieu Date: Sun, 8 Sep 2024 15:43:20 +0200 Subject: [PATCH] rust: a question mark operator is a conditional exit A question mark operator `?` may exit the current function the same way a `return` will. It must be counted in the "nexits" statistics. --- src/metrics/exit.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/metrics/exit.rs b/src/metrics/exit.rs index a98509824..8e4128a79 100644 --- a/src/metrics/exit.rs +++ b/src/metrics/exit.rs @@ -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; @@ -222,6 +222,23 @@ mod tests { }); } + #[test] + fn rust_question_mark() { + check_metrics::("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::("int a = 42;", "foo.c", |metric| {