Skip to content

Commit

Permalink
Auto merge of #13187 - matklad:return, r=Veykril
Browse files Browse the repository at this point in the history
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 :-)
  • Loading branch information
bors committed Sep 4, 2022
2 parents 8ddb8b7 + d7ef3f5 commit 67920f7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
24 changes: 18 additions & 6 deletions crates/ide-completion/src/completions/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,26 @@ pub(crate) fn complete_expr_path(
}
}

if let Some(ty) = innermost_ret_ty {
if let Some(ret_ty) = innermost_ret_ty {
add_keyword(
"return",
match (in_block_expr, ty.is_unit()) {
(true, true) => "return ;",
(true, false) => "return;",
(false, true) => "return $0",
(false, false) => "return",
match (ret_ty.is_unit(), in_block_expr) {
(true, true) => {
cov_mark::hit!(return_unit_block);
"return;"
}
(true, false) => {
cov_mark::hit!(return_unit_no_block);
"return"
}
(false, true) => {
cov_mark::hit!(return_value_block);
"return $0;"
}
(false, false) => {
cov_mark::hit!(return_value_no_block);
"return $0"
}
},
);
}
Expand Down
38 changes: 37 additions & 1 deletion crates/ide-completion/src/tests/expression.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Completion tests for expressions.
use expect_test::{expect, Expect};

use crate::tests::{completion_list, BASE_ITEMS_FIXTURE};
use crate::tests::{check_edit, completion_list, BASE_ITEMS_FIXTURE};

fn check(ra_fixture: &str, expect: Expect) {
let actual = completion_list(&format!("{}{}", BASE_ITEMS_FIXTURE, ra_fixture));
Expand Down Expand Up @@ -670,3 +670,39 @@ fn main() {
"#]],
);
}

#[test]
fn return_unit_block() {
cov_mark::check!(return_unit_block);
check_edit("return", r#"fn f() { if true { $0 } }"#, r#"fn f() { if true { return; } }"#);
}

#[test]
fn return_unit_no_block() {
cov_mark::check!(return_unit_no_block);
check_edit(
"return",
r#"fn f() { match () { () => $0 } }"#,
r#"fn f() { match () { () => return } }"#,
);
}

#[test]
fn return_value_block() {
cov_mark::check!(return_value_block);
check_edit(
"return",
r#"fn f() -> i32 { if true { $0 } }"#,
r#"fn f() -> i32 { if true { return $0; } }"#,
);
}

#[test]
fn return_value_no_block() {
cov_mark::check!(return_value_no_block);
check_edit(
"return",
r#"fn f() -> i32 { match () { () => $0 } }"#,
r#"fn f() -> i32 { match () { () => return $0 } }"#,
);
}

0 comments on commit 67920f7

Please sign in to comment.