Merge pull request #3322 from fyrchik/fix/3304
calculate statement first line properly
This commit is contained in:
commit
4ed31b606d
12
src/attr.rs
12
src/attr.rs
@ -34,6 +34,18 @@ pub fn get_attrs_from_stmt(stmt: &ast::Stmt) -> &[ast::Attribute] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_span_without_attrs(stmt: &ast::Stmt) -> Span {
|
||||||
|
match stmt.node {
|
||||||
|
ast::StmtKind::Local(ref local) => local.span,
|
||||||
|
ast::StmtKind::Item(ref item) => item.span,
|
||||||
|
ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => expr.span,
|
||||||
|
ast::StmtKind::Mac(ref mac) => {
|
||||||
|
let (ref mac, _, _) = **mac;
|
||||||
|
mac.span
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns attributes that are within `outer_span`.
|
/// Returns attributes that are within `outer_span`.
|
||||||
pub fn filter_inline_attrs(attrs: &[ast::Attribute], outer_span: Span) -> Vec<ast::Attribute> {
|
pub fn filter_inline_attrs(attrs: &[ast::Attribute], outer_span: Span) -> Vec<ast::Attribute> {
|
||||||
attrs
|
attrs
|
||||||
|
@ -113,7 +113,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
|||||||
ast::StmtKind::Local(..) | ast::StmtKind::Expr(..) | ast::StmtKind::Semi(..) => {
|
ast::StmtKind::Local(..) | ast::StmtKind::Expr(..) | ast::StmtKind::Semi(..) => {
|
||||||
let attrs = get_attrs_from_stmt(stmt);
|
let attrs = get_attrs_from_stmt(stmt);
|
||||||
if contains_skip(attrs) {
|
if contains_skip(attrs) {
|
||||||
self.push_skipped_with_span(attrs, stmt.span());
|
self.push_skipped_with_span(attrs, stmt.span(), get_span_without_attrs(stmt));
|
||||||
} else {
|
} else {
|
||||||
let shape = self.shape();
|
let shape = self.shape();
|
||||||
let rewrite = self.with_context(|ctx| stmt.rewrite(&ctx, shape));
|
let rewrite = self.with_context(|ctx| stmt.rewrite(&ctx, shape));
|
||||||
@ -123,7 +123,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
|||||||
ast::StmtKind::Mac(ref mac) => {
|
ast::StmtKind::Mac(ref mac) => {
|
||||||
let (ref mac, _macro_style, ref attrs) = **mac;
|
let (ref mac, _macro_style, ref attrs) = **mac;
|
||||||
if self.visit_attrs(attrs, ast::AttrStyle::Outer) {
|
if self.visit_attrs(attrs, ast::AttrStyle::Outer) {
|
||||||
self.push_skipped_with_span(attrs, stmt.span());
|
self.push_skipped_with_span(attrs, stmt.span(), get_span_without_attrs(stmt));
|
||||||
} else {
|
} else {
|
||||||
self.visit_mac(mac, None, MacroPosition::Statement);
|
self.visit_mac(mac, None, MacroPosition::Statement);
|
||||||
}
|
}
|
||||||
@ -331,14 +331,14 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
|||||||
// For use items, skip rewriting attributes. Just check for a skip attribute.
|
// For use items, skip rewriting attributes. Just check for a skip attribute.
|
||||||
ast::ItemKind::Use(..) => {
|
ast::ItemKind::Use(..) => {
|
||||||
if contains_skip(attrs) {
|
if contains_skip(attrs) {
|
||||||
self.push_skipped_with_span(attrs.as_slice(), item.span());
|
self.push_skipped_with_span(attrs.as_slice(), item.span(), item.span());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Module is inline, in this case we treat it like any other item.
|
// Module is inline, in this case we treat it like any other item.
|
||||||
_ if !is_mod_decl(item) => {
|
_ if !is_mod_decl(item) => {
|
||||||
if self.visit_attrs(&item.attrs, ast::AttrStyle::Outer) {
|
if self.visit_attrs(&item.attrs, ast::AttrStyle::Outer) {
|
||||||
self.push_skipped_with_span(item.attrs.as_slice(), item.span());
|
self.push_skipped_with_span(item.attrs.as_slice(), item.span(), item.span());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -357,7 +357,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
|||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
if self.visit_attrs(&item.attrs, ast::AttrStyle::Outer) {
|
if self.visit_attrs(&item.attrs, ast::AttrStyle::Outer) {
|
||||||
self.push_skipped_with_span(item.attrs.as_slice(), item.span());
|
self.push_skipped_with_span(item.attrs.as_slice(), item.span(), item.span());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -474,7 +474,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
|||||||
skip_out_of_file_lines_range_visitor!(self, ti.span);
|
skip_out_of_file_lines_range_visitor!(self, ti.span);
|
||||||
|
|
||||||
if self.visit_attrs(&ti.attrs, ast::AttrStyle::Outer) {
|
if self.visit_attrs(&ti.attrs, ast::AttrStyle::Outer) {
|
||||||
self.push_skipped_with_span(ti.attrs.as_slice(), ti.span());
|
self.push_skipped_with_span(ti.attrs.as_slice(), ti.span(), ti.span());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -518,7 +518,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
|||||||
skip_out_of_file_lines_range_visitor!(self, ii.span);
|
skip_out_of_file_lines_range_visitor!(self, ii.span);
|
||||||
|
|
||||||
if self.visit_attrs(&ii.attrs, ast::AttrStyle::Outer) {
|
if self.visit_attrs(&ii.attrs, ast::AttrStyle::Outer) {
|
||||||
self.push_skipped_with_span(ii.attrs.as_slice(), ii.span());
|
self.push_skipped_with_span(ii.attrs.as_slice(), ii.span(), ii.span());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -592,7 +592,12 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
|||||||
self.push_rewrite_inner(span, rewrite);
|
self.push_rewrite_inner(span, rewrite);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn push_skipped_with_span(&mut self, attrs: &[ast::Attribute], item_span: Span) {
|
pub fn push_skipped_with_span(
|
||||||
|
&mut self,
|
||||||
|
attrs: &[ast::Attribute],
|
||||||
|
item_span: Span,
|
||||||
|
main_span: Span,
|
||||||
|
) {
|
||||||
self.format_missing_with_indent(source!(self, item_span).lo());
|
self.format_missing_with_indent(source!(self, item_span).lo());
|
||||||
// do not take into account the lines with attributes as part of the skipped range
|
// do not take into account the lines with attributes as part of the skipped range
|
||||||
let attrs_end = attrs
|
let attrs_end = attrs
|
||||||
@ -600,8 +605,11 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
|||||||
.map(|attr| self.source_map.lookup_char_pos(attr.span().hi()).line)
|
.map(|attr| self.source_map.lookup_char_pos(attr.span().hi()).line)
|
||||||
.max()
|
.max()
|
||||||
.unwrap_or(1);
|
.unwrap_or(1);
|
||||||
// Add 1 to get the line past the last attribute
|
let first_line = self.source_map.lookup_char_pos(main_span.lo()).line;
|
||||||
let lo = attrs_end + 1;
|
// Statement can start after some newlines and/or spaces
|
||||||
|
// or it can be on the same line as the last attribute.
|
||||||
|
// So here we need to take a minimum between the two.
|
||||||
|
let lo = std::cmp::min(attrs_end + 1, first_line);
|
||||||
self.push_rewrite_inner(item_span, None);
|
self.push_rewrite_inner(item_span, None);
|
||||||
let hi = self.line_number + 1;
|
let hi = self.line_number + 1;
|
||||||
self.skipped_range.push((lo, hi));
|
self.skipped_range.push((lo, hi));
|
||||||
|
42
tests/target/issue-3304.rs
Normal file
42
tests/target/issue-3304.rs
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
// rustfmt-error_on_line_overflow: true
|
||||||
|
|
||||||
|
#[rustfmt::skip] use one::two::three::four::five::six::seven::eight::night::ten::eleven::twelve::thirteen::fourteen::fiveteen;
|
||||||
|
#[rustfmt::skip]
|
||||||
|
|
||||||
|
use one::two::three::four::five::six::seven::eight::night::ten::eleven::twelve::thirteen::fourteen::fiveteen;
|
||||||
|
|
||||||
|
macro_rules! test_macro {
|
||||||
|
($($id:ident),*) => {};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! test_macro2 {
|
||||||
|
($($id:ident),*) => {
|
||||||
|
1
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
#[rustfmt::skip] test_macro! { one, two, three, four, five, six, seven, eight, night, ten, eleven, twelve, thirteen, fourteen, fiveteen };
|
||||||
|
#[rustfmt::skip]
|
||||||
|
|
||||||
|
test_macro! { one, two, three, four, five, six, seven, eight, night, ten, eleven, twelve, thirteen, fourteen, fiveteen };
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_local() {
|
||||||
|
#[rustfmt::skip] let x = test_macro! { one, two, three, four, five, six, seven, eight, night, ten, eleven, twelve, thirteen, fourteen, fiveteen };
|
||||||
|
#[rustfmt::skip]
|
||||||
|
|
||||||
|
let x = test_macro! { one, two, three, four, five, six, seven, eight, night, ten, eleven, twelve, thirteen, fourteen, fiveteen };
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_expr(_: [u32]) -> u32 {
|
||||||
|
#[rustfmt::skip] test_expr([9999999999999, 9999999999999, 9999999999999, 9999999999999, 9999999999999, 9999999999999, 9999999999999, 9999999999999]);
|
||||||
|
#[rustfmt::skip]
|
||||||
|
|
||||||
|
test_expr([9999999999999, 9999999999999, 9999999999999, 9999999999999, 9999999999999, 9999999999999, 9999999999999, 9999999999999])
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rustfmt::skip] mod test { use one::two::three::four::five::six::seven::eight::night::ten::eleven::twelve::thirteen::fourteen::fiveteen; }
|
||||||
|
#[rustfmt::skip]
|
||||||
|
|
||||||
|
mod test { use one::two::three::four::five::six::seven::eight::night::ten::eleven::twelve::thirteen::fourteen::fiveteen; }
|
Loading…
x
Reference in New Issue
Block a user