Skip to content

Commit 67920f7

Browse files
committed
Auto merge of rust-lang#13187 - matklad:return, r=Veykril
fix: correct broken logic for return complition It seems that we've accidentally deleted the tests here couple of years ago, and then fairly recently made a typo during refactor as well. Reinstall tests, with coverage marks this time :-)
2 parents 8ddb8b7 + d7ef3f5 commit 67920f7

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

crates/ide-completion/src/completions/expr.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,26 @@ pub(crate) fn complete_expr_path(
282282
}
283283
}
284284

285-
if let Some(ty) = innermost_ret_ty {
285+
if let Some(ret_ty) = innermost_ret_ty {
286286
add_keyword(
287287
"return",
288-
match (in_block_expr, ty.is_unit()) {
289-
(true, true) => "return ;",
290-
(true, false) => "return;",
291-
(false, true) => "return $0",
292-
(false, false) => "return",
288+
match (ret_ty.is_unit(), in_block_expr) {
289+
(true, true) => {
290+
cov_mark::hit!(return_unit_block);
291+
"return;"
292+
}
293+
(true, false) => {
294+
cov_mark::hit!(return_unit_no_block);
295+
"return"
296+
}
297+
(false, true) => {
298+
cov_mark::hit!(return_value_block);
299+
"return $0;"
300+
}
301+
(false, false) => {
302+
cov_mark::hit!(return_value_no_block);
303+
"return $0"
304+
}
293305
},
294306
);
295307
}

crates/ide-completion/src/tests/expression.rs

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Completion tests for expressions.
22
use expect_test::{expect, Expect};
33

4-
use crate::tests::{completion_list, BASE_ITEMS_FIXTURE};
4+
use crate::tests::{check_edit, completion_list, BASE_ITEMS_FIXTURE};
55

66
fn check(ra_fixture: &str, expect: Expect) {
77
let actual = completion_list(&format!("{}{}", BASE_ITEMS_FIXTURE, ra_fixture));
@@ -670,3 +670,39 @@ fn main() {
670670
"#]],
671671
);
672672
}
673+
674+
#[test]
675+
fn return_unit_block() {
676+
cov_mark::check!(return_unit_block);
677+
check_edit("return", r#"fn f() { if true { $0 } }"#, r#"fn f() { if true { return; } }"#);
678+
}
679+
680+
#[test]
681+
fn return_unit_no_block() {
682+
cov_mark::check!(return_unit_no_block);
683+
check_edit(
684+
"return",
685+
r#"fn f() { match () { () => $0 } }"#,
686+
r#"fn f() { match () { () => return } }"#,
687+
);
688+
}
689+
690+
#[test]
691+
fn return_value_block() {
692+
cov_mark::check!(return_value_block);
693+
check_edit(
694+
"return",
695+
r#"fn f() -> i32 { if true { $0 } }"#,
696+
r#"fn f() -> i32 { if true { return $0; } }"#,
697+
);
698+
}
699+
700+
#[test]
701+
fn return_value_no_block() {
702+
cov_mark::check!(return_value_no_block);
703+
check_edit(
704+
"return",
705+
r#"fn f() -> i32 { match () { () => $0 } }"#,
706+
r#"fn f() -> i32 { match () { () => return $0 } }"#,
707+
);
708+
}

0 commit comments

Comments
 (0)