Skip to content

Commit b035001

Browse files
authoredSep 13, 2024··
rust: a question mark operator is a conditional exit (#1113)
A question mark operator `?` may exit the current function the same way a `return` will. It must be counted in the "nexits" statistics.
1 parent e0a5c03 commit b035001

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed
 

‎src/metrics/exit.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl Exit for TsxCode {
156156

157157
impl Exit for RustCode {
158158
fn compute(node: &Node, stats: &mut Stats) {
159-
if matches!(node.kind_id().into(), Rust::ReturnExpression)
159+
if matches!(node.kind_id().into(), Rust::ReturnExpression | Rust::QMARK)
160160
|| Self::is_func(node) && node.child_by_field_name("return_type").is_some()
161161
{
162162
stats.exit += 1;
@@ -222,6 +222,23 @@ mod tests {
222222
});
223223
}
224224

225+
#[test]
226+
fn rust_question_mark() {
227+
check_metrics::<RustParser>("let _ = a? + b? + c?;", "foo.rs", |metric| {
228+
// 0 functions
229+
insta::assert_json_snapshot!(
230+
metric.nexits,
231+
@r###"
232+
{
233+
"sum": 3.0,
234+
"average": null,
235+
"min": 3.0,
236+
"max": 3.0
237+
}"###
238+
);
239+
});
240+
}
241+
225242
#[test]
226243
fn c_no_exit() {
227244
check_metrics::<CppParser>("int a = 42;", "foo.c", |metric| {

0 commit comments

Comments
 (0)
Please sign in to comment.