Skip to content

Commit 9c0f9d0

Browse files
author
Côme ALLART
committed
feat: trait fn: add panicking example only if default panicks
1 parent f5e0998 commit 9c0f9d0

File tree

1 file changed

+121
-1
lines changed

1 file changed

+121
-1
lines changed

crates/ide_assists/src/handlers/generate_documentation_template.rs

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ fn introduction_builder(ast_func: &ast::Fn) -> String {
9696
fn examples_builder(ast_func: &ast::Fn, ctx: &AssistContext) -> Option<Vec<String>> {
9797
let (no_panic_ex, panic_ex) = if is_in_trait_def(ast_func, ctx) {
9898
let message = "// Example template not implemented for trait functions";
99-
(Some(vec![message.into()]), Some(vec![message.into()]))
99+
let panic_ex = match can_panic(ast_func) {
100+
Some(true) => Some(vec![message.into()]),
101+
_ => None,
102+
};
103+
(Some(vec![message.into()]), panic_ex)
100104
} else {
101105
let panic_ex = match can_panic(ast_func) {
102106
Some(true) => gen_panic_ex_template(ast_func, ctx),
@@ -912,6 +916,122 @@ impl MyStruct {
912916
);
913917
}
914918

919+
#[test]
920+
fn supports_fn_in_trait() {
921+
check_assist(
922+
generate_documentation_template,
923+
r#"
924+
pub trait MyTrait {
925+
fn fun$0ction_trait();
926+
}
927+
"#,
928+
r#"
929+
pub trait MyTrait {
930+
/// .
931+
///
932+
/// # Examples
933+
///
934+
/// ```
935+
/// // Example template not implemented for trait functions
936+
/// ```
937+
fn function_trait();
938+
}
939+
"#,
940+
);
941+
}
942+
943+
#[test]
944+
fn supports_unsafe_fn_in_trait() {
945+
check_assist(
946+
generate_documentation_template,
947+
r#"
948+
pub trait MyTrait {
949+
unsafe fn unsafe_funct$0ion_trait();
950+
}
951+
"#,
952+
r#"
953+
pub trait MyTrait {
954+
/// .
955+
///
956+
/// # Examples
957+
///
958+
/// ```
959+
/// // Example template not implemented for trait functions
960+
/// ```
961+
///
962+
/// # Safety
963+
///
964+
/// .
965+
unsafe fn unsafe_function_trait();
966+
}
967+
"#,
968+
);
969+
}
970+
971+
#[test]
972+
fn supports_fn_in_trait_with_default_panicking() {
973+
check_assist(
974+
generate_documentation_template,
975+
r#"
976+
pub trait MyTrait {
977+
fn function_trait_with_$0default_panicking() {
978+
panic!()
979+
}
980+
}
981+
"#,
982+
r#"
983+
pub trait MyTrait {
984+
/// .
985+
///
986+
/// # Examples
987+
///
988+
/// ```
989+
/// // Example template not implemented for trait functions
990+
/// ```
991+
///
992+
/// ```should_panic
993+
/// // Example template not implemented for trait functions
994+
/// ```
995+
///
996+
/// # Panics
997+
///
998+
/// Panics if .
999+
fn function_trait_with_default_panicking() {
1000+
panic!()
1001+
}
1002+
}
1003+
"#,
1004+
);
1005+
}
1006+
1007+
#[test]
1008+
fn supports_fn_in_trait_returning_result() {
1009+
check_assist(
1010+
generate_documentation_template,
1011+
r#"
1012+
pub trait MyTrait {
1013+
fn function_tr$0ait_returning_result() -> Result<(), std::io::Error>;
1014+
}
1015+
"#,
1016+
r#"
1017+
pub trait MyTrait {
1018+
/// .
1019+
///
1020+
/// # Examples
1021+
///
1022+
/// ```
1023+
/// // Example template not implemented for trait functions
1024+
/// ```
1025+
///
1026+
/// # Errors
1027+
///
1028+
/// This function will return an error if .
1029+
fn function_trait_returning_result() -> Result<(), std::io::Error>;
1030+
}
1031+
"#,
1032+
);
1033+
}
1034+
9151035
#[test]
9161036
fn detects_new() {
9171037
check_assist(

0 commit comments

Comments
 (0)