Skip to content

Commit e628c66

Browse files
bors[bot]bnjjj
andauthored
Merge #4945
4945: do not suggest assist for return type to result in bad case r=matklad a=bnjjj close #4826 Co-authored-by: Benjamin Coenen <[email protected]>
2 parents e07826b + 9342574 commit e628c66

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

crates/ra_assists/src/handlers/change_return_type_to_result.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use ra_syntax::{
44
};
55

66
use crate::{AssistContext, AssistId, Assists};
7+
use test_utils::mark;
78

89
// Assist: change_return_type_to_result
910
//
@@ -22,8 +23,13 @@ pub(crate) fn change_return_type_to_result(acc: &mut Assists, ctx: &AssistContex
2223
let fn_def = ret_type.syntax().parent().and_then(ast::FnDef::cast)?;
2324

2425
let type_ref = &ret_type.type_ref()?;
25-
if type_ref.syntax().text().to_string().starts_with("Result<") {
26-
return None;
26+
let ret_type_str = type_ref.syntax().text().to_string();
27+
let first_part_ret_type = ret_type_str.splitn(2, '<').next();
28+
if let Some(ret_type_first_part) = first_part_ret_type {
29+
if ret_type_first_part.ends_with("Result") {
30+
mark::hit!(change_return_type_to_result_simple_return_type_already_result);
31+
return None;
32+
}
2733
}
2834

2935
let block_expr = &fn_def.body()?;
@@ -296,6 +302,29 @@ mod tests {
296302
);
297303
}
298304

305+
#[test]
306+
fn change_return_type_to_result_simple_return_type_already_result_std() {
307+
check_assist_not_applicable(
308+
change_return_type_to_result,
309+
r#"fn foo() -> std::result::Result<i32<|>, String> {
310+
let test = "test";
311+
return 42i32;
312+
}"#,
313+
);
314+
}
315+
316+
#[test]
317+
fn change_return_type_to_result_simple_return_type_already_result() {
318+
mark::check!(change_return_type_to_result_simple_return_type_already_result);
319+
check_assist_not_applicable(
320+
change_return_type_to_result,
321+
r#"fn foo() -> Result<i32<|>, String> {
322+
let test = "test";
323+
return 42i32;
324+
}"#,
325+
);
326+
}
327+
299328
#[test]
300329
fn change_return_type_to_result_simple_with_cursor() {
301330
check_assist(

0 commit comments

Comments
 (0)