Skip to content

Commit 0e89f2f

Browse files
author
Côme ALLART
committed
feat: remove should_panic example generation
1 parent 9e53db2 commit 0e89f2f

File tree

1 file changed

+24
-97
lines changed

1 file changed

+24
-97
lines changed

crates/ide_assists/src/handlers/generate_documentation_template.rs

Lines changed: 24 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -101,31 +101,14 @@ fn introduction_builder(ast_func: &ast::Fn, ctx: &AssistContext) -> String {
101101

102102
/// Builds an `# Examples` section. An option is returned to be able to manage an error in the AST.
103103
fn examples_builder(ast_func: &ast::Fn, ctx: &AssistContext) -> Option<Vec<String>> {
104-
let (no_panic_ex, panic_ex) = if is_in_trait_def(ast_func, ctx) {
105-
let message = "// Example template not implemented for trait functions";
106-
let panic_ex = match can_panic(ast_func) {
107-
Some(true) => Some(vec![message.into()]),
108-
_ => None,
109-
};
110-
(Some(vec![message.into()]), panic_ex)
104+
let mut lines = string_vec_from(&["# Examples", "", "```"]);
105+
if is_in_trait_def(ast_func, ctx) {
106+
lines.push("// Example template not implemented for trait functions".into());
111107
} else {
112-
let panic_ex = match can_panic(ast_func) {
113-
Some(true) => gen_panic_ex_template(ast_func, ctx),
114-
_ => None,
115-
};
116-
let no_panic_ex = gen_ex_template(ast_func, ctx);
117-
(no_panic_ex, panic_ex)
108+
lines.append(&mut gen_ex_template(ast_func, ctx)?)
118109
};
119110

120-
let mut lines = string_vec_from(&["# Examples", "", "```"]);
121-
lines.append(&mut no_panic_ex?);
122111
lines.push("```".into());
123-
if let Some(mut ex) = panic_ex {
124-
lines.push("".into());
125-
lines.push("```should_panic".into());
126-
lines.append(&mut ex);
127-
lines.push("```".into());
128-
}
129112
Some(lines)
130113
}
131114

@@ -154,53 +137,8 @@ fn safety_builder(ast_func: &ast::Fn) -> Option<Vec<String>> {
154137
}
155138
}
156139

157-
/// Generate an example template which should not panic
158-
/// `None` if the function has a `self` parameter but is not in an `impl`.
140+
/// Generates an example template
159141
fn gen_ex_template(ast_func: &ast::Fn, ctx: &AssistContext) -> Option<Vec<String>> {
160-
let (mut lines, ex_helper) = gen_ex_start_helper(ast_func, ctx)?;
161-
// Call the function, check result
162-
if returns_a_value(ast_func, ctx) {
163-
if count_parameters(&ex_helper.param_list) < 3 {
164-
lines.push(format!("assert_eq!({}, );", ex_helper.function_call));
165-
} else {
166-
lines.push(format!("let result = {};", ex_helper.function_call));
167-
lines.push("assert_eq!(result, );".into());
168-
}
169-
} else {
170-
lines.push(format!("{};", ex_helper.function_call));
171-
}
172-
// Check the mutated values
173-
if is_ref_mut_self(ast_func) == Some(true) {
174-
lines.push(format!("assert_eq!({}, );", ex_helper.self_name?));
175-
}
176-
for param_name in &ex_helper.ref_mut_params {
177-
lines.push(format!("assert_eq!({}, );", param_name));
178-
}
179-
Some(lines)
180-
}
181-
182-
/// Generate an example template which should panic
183-
/// `None` if the function has a `self` parameter but is not in an `impl`.
184-
fn gen_panic_ex_template(ast_func: &ast::Fn, ctx: &AssistContext) -> Option<Vec<String>> {
185-
let (mut lines, ex_helper) = gen_ex_start_helper(ast_func, ctx)?;
186-
match returns_a_value(ast_func, ctx) {
187-
true => lines.push(format!("let _ = {}; // panics", ex_helper.function_call)),
188-
false => lines.push(format!("{}; // panics", ex_helper.function_call)),
189-
}
190-
Some(lines)
191-
}
192-
193-
/// Intermediary results of the start of example generation
194-
struct ExHelper {
195-
function_call: String,
196-
param_list: ast::ParamList,
197-
ref_mut_params: Vec<String>,
198-
self_name: Option<String>,
199-
}
200-
201-
/// Builds the start of the example and transmit the useful intermediary results.
202-
/// `None` if the function has a `self` parameter but is not in an `impl`.
203-
fn gen_ex_start_helper(ast_func: &ast::Fn, ctx: &AssistContext) -> Option<(Vec<String>, ExHelper)> {
204142
let mut lines = Vec::new();
205143
let is_unsafe = ast_func.unsafe_token().is_some();
206144
let param_list = ast_func.param_list()?;
@@ -215,9 +153,26 @@ fn gen_ex_start_helper(ast_func: &ast::Fn, ctx: &AssistContext) -> Option<(Vec<S
215153
for param_name in &ref_mut_params {
216154
lines.push(format!("let mut {} = ;", param_name))
217155
}
156+
// Call the function, check result
218157
let function_call = function_call(ast_func, &param_list, self_name.as_deref(), is_unsafe)?;
219-
let ex_helper = ExHelper { function_call, param_list, ref_mut_params, self_name };
220-
Some((lines, ex_helper))
158+
if returns_a_value(ast_func, ctx) {
159+
if count_parameters(&param_list) < 3 {
160+
lines.push(format!("assert_eq!({}, );", function_call));
161+
} else {
162+
lines.push(format!("let result = {};", function_call));
163+
lines.push("assert_eq!(result, );".into());
164+
}
165+
} else {
166+
lines.push(format!("{};", function_call));
167+
}
168+
// Check the mutated values
169+
if is_ref_mut_self(ast_func) == Some(true) {
170+
lines.push(format!("assert_eq!({}, );", self_name?));
171+
}
172+
for param_name in &ref_mut_params {
173+
lines.push(format!("assert_eq!({}, );", param_name));
174+
}
175+
Some(lines)
221176
}
222177

223178
/// Checks if the function is public / exported
@@ -616,12 +571,6 @@ pub fn panic$0s_if(a: bool) {
616571
/// panics_if(a);
617572
/// ```
618573
///
619-
/// ```should_panic
620-
/// use test::panics_if;
621-
///
622-
/// panics_if(a); // panics
623-
/// ```
624-
///
625574
/// # Panics
626575
///
627576
/// Panics if .
@@ -654,12 +603,6 @@ pub fn $0panics_if_not(a: bool) {
654603
/// panics_if_not(a);
655604
/// ```
656605
///
657-
/// ```should_panic
658-
/// use test::panics_if_not;
659-
///
660-
/// panics_if_not(a); // panics
661-
/// ```
662-
///
663606
/// # Panics
664607
///
665608
/// Panics if .
@@ -690,12 +633,6 @@ pub fn $0panics_if_none(a: Option<()>) {
690633
/// panics_if_none(a);
691634
/// ```
692635
///
693-
/// ```should_panic
694-
/// use test::panics_if_none;
695-
///
696-
/// panics_if_none(a); // panics
697-
/// ```
698-
///
699636
/// # Panics
700637
///
701638
/// Panics if .
@@ -726,12 +663,6 @@ pub fn $0panics_if_none2(a: Option<()>) {
726663
/// panics_if_none2(a);
727664
/// ```
728665
///
729-
/// ```should_panic
730-
/// use test::panics_if_none2;
731-
///
732-
/// panics_if_none2(a); // panics
733-
/// ```
734-
///
735666
/// # Panics
736667
///
737668
/// Panics if .
@@ -984,10 +915,6 @@ pub trait MyTrait {
984915
/// // Example template not implemented for trait functions
985916
/// ```
986917
///
987-
/// ```should_panic
988-
/// // Example template not implemented for trait functions
989-
/// ```
990-
///
991918
/// # Panics
992919
///
993920
/// Panics if .

0 commit comments

Comments
 (0)