From 4c89d32f7a7842fa58ed4460c0f455b5fda8f47b Mon Sep 17 00:00:00 2001 From: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> Date: Thu, 18 Jun 2020 22:16:39 +0200 Subject: do not suggest assist for return type to result in bad case #4826 Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com> --- .../src/handlers/change_return_type_to_result.rs | 30 ++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'crates/ra_assists/src') diff --git a/crates/ra_assists/src/handlers/change_return_type_to_result.rs b/crates/ra_assists/src/handlers/change_return_type_to_result.rs index c6baa0a57..12572936a 100644 --- a/crates/ra_assists/src/handlers/change_return_type_to_result.rs +++ b/crates/ra_assists/src/handlers/change_return_type_to_result.rs @@ -22,8 +22,12 @@ pub(crate) fn change_return_type_to_result(acc: &mut Assists, ctx: &AssistContex let fn_def = ret_type.syntax().parent().and_then(ast::FnDef::cast)?; let type_ref = &ret_type.type_ref()?; - if type_ref.syntax().text().to_string().starts_with("Result<") { - return None; + let ret_type_str = type_ref.syntax().text().to_string(); + let first_part_ret_type = ret_type_str.splitn(2, '<').next(); + if let Some(ret_type_first_part) = first_part_ret_type { + if ret_type_first_part.ends_with("Result") { + return None; + } } let block_expr = &fn_def.body()?; @@ -296,6 +300,28 @@ mod tests { ); } + #[test] + fn change_return_type_to_result_simple_return_type_already_result_std() { + check_assist_not_applicable( + change_return_type_to_result, + r#"fn foo() -> std::result::Result, String> { + let test = "test"; + return 42i32; + }"#, + ); + } + + #[test] + fn change_return_type_to_result_simple_return_type_already_result() { + check_assist_not_applicable( + change_return_type_to_result, + r#"fn foo() -> Result, String> { + let test = "test"; + return 42i32; + }"#, + ); + } + #[test] fn change_return_type_to_result_simple_with_cursor() { check_assist( -- cgit v1.2.3