@@ -101,31 +101,14 @@ fn introduction_builder(ast_func: &ast::Fn, ctx: &AssistContext) -> String {
101
101
102
102
/// Builds an `# Examples` section. An option is returned to be able to manage an error in the AST.
103
103
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 ( ) ) ;
111
107
} 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) ?)
118
109
} ;
119
110
120
- let mut lines = string_vec_from ( & [ "# Examples" , "" , "```" ] ) ;
121
- lines. append ( & mut no_panic_ex?) ;
122
111
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
- }
129
112
Some ( lines)
130
113
}
131
114
@@ -154,53 +137,8 @@ fn safety_builder(ast_func: &ast::Fn) -> Option<Vec<String>> {
154
137
}
155
138
}
156
139
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
159
141
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 ) > {
204
142
let mut lines = Vec :: new ( ) ;
205
143
let is_unsafe = ast_func. unsafe_token ( ) . is_some ( ) ;
206
144
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
215
153
for param_name in & ref_mut_params {
216
154
lines. push ( format ! ( "let mut {} = ;" , param_name) )
217
155
}
156
+ // Call the function, check result
218
157
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)
221
176
}
222
177
223
178
/// Checks if the function is public / exported
@@ -616,12 +571,6 @@ pub fn panic$0s_if(a: bool) {
616
571
/// panics_if(a);
617
572
/// ```
618
573
///
619
- /// ```should_panic
620
- /// use test::panics_if;
621
- ///
622
- /// panics_if(a); // panics
623
- /// ```
624
- ///
625
574
/// # Panics
626
575
///
627
576
/// Panics if .
@@ -654,12 +603,6 @@ pub fn $0panics_if_not(a: bool) {
654
603
/// panics_if_not(a);
655
604
/// ```
656
605
///
657
- /// ```should_panic
658
- /// use test::panics_if_not;
659
- ///
660
- /// panics_if_not(a); // panics
661
- /// ```
662
- ///
663
606
/// # Panics
664
607
///
665
608
/// Panics if .
@@ -690,12 +633,6 @@ pub fn $0panics_if_none(a: Option<()>) {
690
633
/// panics_if_none(a);
691
634
/// ```
692
635
///
693
- /// ```should_panic
694
- /// use test::panics_if_none;
695
- ///
696
- /// panics_if_none(a); // panics
697
- /// ```
698
- ///
699
636
/// # Panics
700
637
///
701
638
/// Panics if .
@@ -726,12 +663,6 @@ pub fn $0panics_if_none2(a: Option<()>) {
726
663
/// panics_if_none2(a);
727
664
/// ```
728
665
///
729
- /// ```should_panic
730
- /// use test::panics_if_none2;
731
- ///
732
- /// panics_if_none2(a); // panics
733
- /// ```
734
- ///
735
666
/// # Panics
736
667
///
737
668
/// Panics if .
@@ -984,10 +915,6 @@ pub trait MyTrait {
984
915
/// // Example template not implemented for trait functions
985
916
/// ```
986
917
///
987
- /// ```should_panic
988
- /// // Example template not implemented for trait functions
989
- /// ```
990
- ///
991
918
/// # Panics
992
919
///
993
920
/// Panics if .
0 commit comments