4945: do not suggest assist for return type to result in bad case  r=matklad a=bnjjj

close #4826

Co-authored-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
This commit is contained in:
bors[bot] 2020-06-26 16:59:59 +00:00 committed by GitHub
commit e628c66359
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,6 +4,7 @@ use ra_syntax::{
};
use crate::{AssistContext, AssistId, Assists};
use test_utils::mark;
// Assist: change_return_type_to_result
//
@ -22,8 +23,13 @@ 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") {
mark::hit!(change_return_type_to_result_simple_return_type_already_result);
return None;
}
}
let block_expr = &fn_def.body()?;
@ -296,6 +302,29 @@ 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<i32<|>, String> {
let test = "test";
return 42i32;
}"#,
);
}
#[test]
fn change_return_type_to_result_simple_return_type_already_result() {
mark::check!(change_return_type_to_result_simple_return_type_already_result);
check_assist_not_applicable(
change_return_type_to_result,
r#"fn foo() -> Result<i32<|>, String> {
let test = "test";
return 42i32;
}"#,
);
}
#[test]
fn change_return_type_to_result_simple_with_cursor() {
check_assist(