about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCôme ALLART <come.allart@etu.emse.fr>2021-12-10 15:53:43 +0100
committerCôme ALLART <come.allart@etu.emse.fr>2021-12-10 15:53:43 +0100
commit9c0f9d02bf9c38e774a2560e4f4f98d433e9dbb6 (patch)
tree7f236afb1e2168f037230588d02032040f1acc04
parentf5e0998402c4087613992092f34f96f0c90b2a86 (diff)
downloadrust-9c0f9d02bf9c38e774a2560e4f4f98d433e9dbb6.tar.gz
rust-9c0f9d02bf9c38e774a2560e4f4f98d433e9dbb6.zip
feat: trait fn: add panicking example only if default panicks
-rw-r--r--crates/ide_assists/src/handlers/generate_documentation_template.rs122
1 files changed, 121 insertions, 1 deletions
diff --git a/crates/ide_assists/src/handlers/generate_documentation_template.rs b/crates/ide_assists/src/handlers/generate_documentation_template.rs
index 6c69f227f7b..113fe7c92c5 100644
--- a/crates/ide_assists/src/handlers/generate_documentation_template.rs
+++ b/crates/ide_assists/src/handlers/generate_documentation_template.rs
@@ -96,7 +96,11 @@ fn introduction_builder(ast_func: &ast::Fn) -> String {
 fn examples_builder(ast_func: &ast::Fn, ctx: &AssistContext) -> Option<Vec<String>> {
     let (no_panic_ex, panic_ex) = if is_in_trait_def(ast_func, ctx) {
         let message = "// Example template not implemented for trait functions";
-        (Some(vec![message.into()]), Some(vec![message.into()]))
+        let panic_ex = match can_panic(ast_func) {
+            Some(true) => Some(vec![message.into()]),
+            _ => None,
+        };
+        (Some(vec![message.into()]), panic_ex)
     } else {
         let panic_ex = match can_panic(ast_func) {
             Some(true) => gen_panic_ex_template(ast_func, ctx),
@@ -913,6 +917,122 @@ impl MyStruct {
     }
 
     #[test]
+    fn supports_fn_in_trait() {
+        check_assist(
+            generate_documentation_template,
+            r#"
+pub trait MyTrait {
+    fn fun$0ction_trait();
+}
+"#,
+            r#"
+pub trait MyTrait {
+    /// .
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// // Example template not implemented for trait functions
+    /// ```
+    fn function_trait();
+}
+"#,
+        );
+    }
+
+    #[test]
+    fn supports_unsafe_fn_in_trait() {
+        check_assist(
+            generate_documentation_template,
+            r#"
+pub trait MyTrait {
+    unsafe fn unsafe_funct$0ion_trait();
+}
+"#,
+            r#"
+pub trait MyTrait {
+    /// .
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// // Example template not implemented for trait functions
+    /// ```
+    ///
+    /// # Safety
+    ///
+    /// .
+    unsafe fn unsafe_function_trait();
+}
+"#,
+        );
+    }
+
+    #[test]
+    fn supports_fn_in_trait_with_default_panicking() {
+        check_assist(
+            generate_documentation_template,
+            r#"
+pub trait MyTrait {
+    fn function_trait_with_$0default_panicking() {
+        panic!()
+    }
+}
+"#,
+            r#"
+pub trait MyTrait {
+    /// .
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// // Example template not implemented for trait functions
+    /// ```
+    ///
+    /// ```should_panic
+    /// // Example template not implemented for trait functions
+    /// ```
+    ///
+    /// # Panics
+    ///
+    /// Panics if .
+    fn function_trait_with_default_panicking() {
+        panic!()
+    }
+}
+"#,
+        );
+    }
+
+    #[test]
+    fn supports_fn_in_trait_returning_result() {
+        check_assist(
+            generate_documentation_template,
+            r#"
+pub trait MyTrait {
+    fn function_tr$0ait_returning_result() -> Result<(), std::io::Error>;
+}
+"#,
+            r#"
+pub trait MyTrait {
+    /// .
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// // Example template not implemented for trait functions
+    /// ```
+    ///
+    /// # Errors
+    ///
+    /// This function will return an error if .
+    fn function_trait_returning_result() -> Result<(), std::io::Error>;
+}
+"#,
+        );
+    }
+
+    #[test]
     fn detects_new() {
         check_assist(
             generate_documentation_template,