@@ -113,7 +113,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
113
113
ast:: StmtKind :: Local ( ..) | ast:: StmtKind :: Expr ( ..) | ast:: StmtKind :: Semi ( ..) => {
114
114
let attrs = get_attrs_from_stmt ( stmt) ;
115
115
if contains_skip ( attrs) {
116
- self . push_skipped_with_span ( attrs, stmt. span ( ) ) ;
116
+ self . push_skipped_with_span ( attrs, stmt. span ( ) , get_span_without_attrs ( stmt ) ) ;
117
117
} else {
118
118
let shape = self . shape ( ) ;
119
119
let rewrite = self . with_context ( |ctx| stmt. rewrite ( & ctx, shape) ) ;
@@ -123,7 +123,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
123
123
ast:: StmtKind :: Mac ( ref mac) => {
124
124
let ( ref mac, _macro_style, ref attrs) = * * mac;
125
125
if self . visit_attrs ( attrs, ast:: AttrStyle :: Outer ) {
126
- self . push_skipped_with_span ( attrs, stmt. span ( ) ) ;
126
+ self . push_skipped_with_span ( attrs, stmt. span ( ) , get_span_without_attrs ( stmt ) ) ;
127
127
} else {
128
128
self . visit_mac ( mac, None , MacroPosition :: Statement ) ;
129
129
}
@@ -331,14 +331,14 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
331
331
// For use items, skip rewriting attributes. Just check for a skip attribute.
332
332
ast:: ItemKind :: Use ( ..) => {
333
333
if contains_skip ( attrs) {
334
- self . push_skipped_with_span ( attrs. as_slice ( ) , item. span ( ) ) ;
334
+ self . push_skipped_with_span ( attrs. as_slice ( ) , item. span ( ) , item . span ( ) ) ;
335
335
return ;
336
336
}
337
337
}
338
338
// Module is inline, in this case we treat it like any other item.
339
339
_ if !is_mod_decl ( item) => {
340
340
if self . visit_attrs ( & item. attrs , ast:: AttrStyle :: Outer ) {
341
- self . push_skipped_with_span ( item. attrs . as_slice ( ) , item. span ( ) ) ;
341
+ self . push_skipped_with_span ( item. attrs . as_slice ( ) , item. span ( ) , item . span ( ) ) ;
342
342
return ;
343
343
}
344
344
}
@@ -357,7 +357,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
357
357
}
358
358
_ => {
359
359
if self . visit_attrs ( & item. attrs , ast:: AttrStyle :: Outer ) {
360
- self . push_skipped_with_span ( item. attrs . as_slice ( ) , item. span ( ) ) ;
360
+ self . push_skipped_with_span ( item. attrs . as_slice ( ) , item. span ( ) , item . span ( ) ) ;
361
361
return ;
362
362
}
363
363
}
@@ -474,7 +474,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
474
474
skip_out_of_file_lines_range_visitor ! ( self , ti. span) ;
475
475
476
476
if self . visit_attrs ( & ti. attrs , ast:: AttrStyle :: Outer ) {
477
- self . push_skipped_with_span ( ti. attrs . as_slice ( ) , ti. span ( ) ) ;
477
+ self . push_skipped_with_span ( ti. attrs . as_slice ( ) , ti. span ( ) , ti . span ( ) ) ;
478
478
return ;
479
479
}
480
480
@@ -518,7 +518,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
518
518
skip_out_of_file_lines_range_visitor ! ( self , ii. span) ;
519
519
520
520
if self . visit_attrs ( & ii. attrs , ast:: AttrStyle :: Outer ) {
521
- self . push_skipped_with_span ( ii. attrs . as_slice ( ) , ii. span ( ) ) ;
521
+ self . push_skipped_with_span ( ii. attrs . as_slice ( ) , ii. span ( ) , ii . span ( ) ) ;
522
522
return ;
523
523
}
524
524
@@ -592,16 +592,24 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
592
592
self . push_rewrite_inner ( span, rewrite) ;
593
593
}
594
594
595
- pub fn push_skipped_with_span ( & mut self , attrs : & [ ast:: Attribute ] , item_span : Span ) {
595
+ pub fn push_skipped_with_span (
596
+ & mut self ,
597
+ attrs : & [ ast:: Attribute ] ,
598
+ item_span : Span ,
599
+ main_span : Span ,
600
+ ) {
596
601
self . format_missing_with_indent ( source ! ( self , item_span) . lo ( ) ) ;
597
602
// do not take into account the lines with attributes as part of the skipped range
598
603
let attrs_end = attrs
599
604
. iter ( )
600
605
. map ( |attr| self . source_map . lookup_char_pos ( attr. span ( ) . hi ( ) ) . line )
601
606
. max ( )
602
607
. unwrap_or ( 1 ) ;
603
- // Add 1 to get the line past the last attribute
604
- let lo = attrs_end + 1 ;
608
+ let first_line = self . source_map . lookup_char_pos ( main_span. lo ( ) ) . line ;
609
+ // Statement can start after some newlines and/or spaces
610
+ // or it can be on the same line as the last attribute.
611
+ // So here we need to take a minimum between the two.
612
+ let lo = std:: cmp:: min ( attrs_end + 1 , first_line) ;
605
613
self . push_rewrite_inner ( item_span, None ) ;
606
614
let hi = self . line_number + 1 ;
607
615
self . skipped_range . push ( ( lo, hi) ) ;
0 commit comments