Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit da2d8a4

Browse files
committed
calculate statement first line properly
1 parent aec0a7c commit da2d8a4

File tree

4 files changed

+54
-10
lines changed

4 files changed

+54
-10
lines changed

src/attr.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ pub fn get_attrs_from_stmt(stmt: &ast::Stmt) -> &[ast::Attribute] {
3434
}
3535
}
3636

37+
pub fn get_span_without_attrs(stmt: &ast::Stmt) -> Span {
38+
match stmt.node {
39+
ast::StmtKind::Local(ref local) => local.span,
40+
ast::StmtKind::Item(ref item) => item.span,
41+
ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => expr.span,
42+
ast::StmtKind::Mac(ref mac) => {
43+
let (ref mac, _, _) = **mac;
44+
mac.span
45+
}
46+
}
47+
}
48+
3749
/// Returns attributes that are within `outer_span`.
3850
pub fn filter_inline_attrs(attrs: &[ast::Attribute], outer_span: Span) -> Vec<ast::Attribute> {
3951
attrs

src/visitor.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
113113
ast::StmtKind::Local(..) | ast::StmtKind::Expr(..) | ast::StmtKind::Semi(..) => {
114114
let attrs = get_attrs_from_stmt(stmt);
115115
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));
117117
} else {
118118
let shape = self.shape();
119119
let rewrite = self.with_context(|ctx| stmt.rewrite(&ctx, shape));
@@ -123,7 +123,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
123123
ast::StmtKind::Mac(ref mac) => {
124124
let (ref mac, _macro_style, ref attrs) = **mac;
125125
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));
127127
} else {
128128
self.visit_mac(mac, None, MacroPosition::Statement);
129129
}
@@ -331,14 +331,14 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
331331
// For use items, skip rewriting attributes. Just check for a skip attribute.
332332
ast::ItemKind::Use(..) => {
333333
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());
335335
return;
336336
}
337337
}
338338
// Module is inline, in this case we treat it like any other item.
339339
_ if !is_mod_decl(item) => {
340340
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());
342342
return;
343343
}
344344
}
@@ -357,7 +357,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
357357
}
358358
_ => {
359359
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());
361361
return;
362362
}
363363
}
@@ -474,7 +474,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
474474
skip_out_of_file_lines_range_visitor!(self, ti.span);
475475

476476
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());
478478
return;
479479
}
480480

@@ -518,7 +518,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
518518
skip_out_of_file_lines_range_visitor!(self, ii.span);
519519

520520
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());
522522
return;
523523
}
524524

@@ -592,16 +592,24 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
592592
self.push_rewrite_inner(span, rewrite);
593593
}
594594

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+
) {
596601
self.format_missing_with_indent(source!(self, item_span).lo());
597602
// do not take into account the lines with attributes as part of the skipped range
598603
let attrs_end = attrs
599604
.iter()
600605
.map(|attr| self.source_map.lookup_char_pos(attr.span().hi()).line)
601606
.max()
602607
.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 two.
612+
let lo = std::cmp::min(attrs_end + 1, first_line);
605613
self.push_rewrite_inner(item_span, None);
606614
let hi = self.line_number + 1;
607615
self.skipped_range.push((lo, hi));

tests/source/issue-3304.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// rustfmt-error_on_line_overflow: true
2+
3+
macro_rules! test_macro {
4+
($($id:ident),*) => {};
5+
}
6+
7+
fn main() {
8+
#[rustfmt::skip] test_macro! { one, two, three, four, five, six, seven, eight, night, ten, eleven, twelve, thirteen, fourteen, fiveteen };
9+
#[rustfmt::skip]
10+
11+
test_macro! { one, two, three, four, five, six, seven, eight, night, ten, eleven, twelve, thirteen, fourteen, fiveteen };
12+
}

tests/target/issue-3304.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// rustfmt-error_on_line_overflow: true
2+
3+
macro_rules! test_macro {
4+
($($id:ident),*) => {};
5+
}
6+
7+
fn main() {
8+
#[rustfmt::skip] test_macro! { one, two, three, four, five, six, seven, eight, night, ten, eleven, twelve, thirteen, fourteen, fiveteen };
9+
#[rustfmt::skip]
10+
11+
test_macro! { one, two, three, four, five, six, seven, eight, night, ten, eleven, twelve, thirteen, fourteen, fiveteen };
12+
}

0 commit comments

Comments
 (0)