@@ -238,6 +238,12 @@ impl LintPass<'_> {
238238}
239239
240240impl ConfDef < ' _ > {
241+ pub fn gen_file ( & mut self , src : & str , dst : & mut String ) {
242+ dst. push_str ( & src[ ..self . decl_sp . range . start as usize ] ) ;
243+ self . gen_mac ( src, dst) ;
244+ dst. push_str ( & src[ self . decl_sp . range . end as usize ..] ) ;
245+ }
246+
241247 pub fn gen_mac ( & mut self , src : & str , dst : & mut String ) {
242248 self . opts . sort_unstable_by_key ( |o| o. name ) ;
243249 dst. push_str ( "define_Conf! {" ) ;
@@ -310,6 +316,7 @@ pub fn gen_sorted_lints_file(
310316 lints : & mut [ ActiveLint < ' _ , ' _ > ] ,
311317 passes : & mut [ LintPass < ' _ > ] ,
312318 ranges : & mut VecBuf < Range < u32 > > ,
319+ copy_src : & mut dyn FnMut ( & str , & mut String ) ,
313320) {
314321 ranges. with ( |ranges| {
315322 ranges. extend ( lints. iter ( ) . map ( |x| x. data . decl_range ) ) ;
@@ -321,7 +328,7 @@ pub fn gen_sorted_lints_file(
321328
322329 let mut ranges = ranges. iter ( ) ;
323330 let pos = if let Some ( range) = ranges. next ( ) {
324- dst . push_str ( & src[ ..range. start as usize ] ) ;
331+ copy_src ( & src[ ..range. start as usize ] , dst ) ;
325332 for lint in & * lints {
326333 lint. gen_mac ( dst) ;
327334 dst. push_str ( "\n \n " ) ;
@@ -333,29 +340,31 @@ pub fn gen_sorted_lints_file(
333340 }
334341 range. end
335342 } else {
336- dst . push_str ( src) ;
343+ copy_src ( src, dst ) ;
337344 return ;
338345 } ;
339346
340347 let pos = ranges. fold ( pos, |start, range| {
341348 let s = & src[ start as usize ..range. start as usize ] ;
342- dst. push_str ( if s. trim_start ( ) . is_empty ( ) {
343- // Only whitespace between this and the previous item. No need to keep that.
344- ""
345- } else if src[ ..pos as usize ] . ends_with ( "\n \n " )
346- && let Some ( s) = s. strip_prefix ( "\n \n " )
347- {
348- // Empty line before and after. Remove one of them.
349- s
350- } else {
351- // Remove only full lines unless something is in the way.
352- s. strip_prefix ( '\n' ) . unwrap_or ( s)
353- } ) ;
349+ // Don't keep whitespace between declarations.
350+ if !s. trim_start ( ) . is_empty ( ) {
351+ let s = if src[ ..pos as usize ] . ends_with ( "\n \n " )
352+ && let Some ( s) = s. strip_prefix ( "\n \n " )
353+ {
354+ // Empty line before and after. Remove one of them.
355+ s
356+ } else {
357+ // Remove the line end immediately proceeding a declaration.
358+ s. strip_prefix ( '\n' ) . unwrap_or ( s)
359+ } ;
360+ copy_src ( s, dst) ;
361+ }
354362 range. end
355363 } ) ;
356364
357365 // Since we always generate an empty line at the end, make sure to always skip it.
358366 let s = & src[ pos as usize ..] ;
359- dst. push_str ( s. strip_prefix ( '\n' ) . map_or ( s, |s| s. strip_prefix ( '\n' ) . unwrap_or ( s) ) ) ;
367+ let s = s. strip_prefix ( '\n' ) . map_or ( s, |s| s. strip_prefix ( '\n' ) . unwrap_or ( s) ) ;
368+ copy_src ( s, dst) ;
360369 } ) ;
361370}
0 commit comments