From 528200009a4778c0295f30282ed2d2ba7f3cd0c0 Mon Sep 17 00:00:00 2001 From: topecongiro Date: Wed, 16 Aug 2017 00:11:20 +0900 Subject: [PATCH] Do not panic against files with invalid attributes --- src/visitor.rs | 17 ++++++----------- tests/source/attrib.rs | 3 +++ tests/target/attrib.rs | 3 +++ 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/visitor.rs b/src/visitor.rs index 6faaf8805b3d..c5d91fee5b3b 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -684,18 +684,13 @@ pub fn visit_attrs(&mut self, attrs: &[ast::Attribute], style: ast::AttrStyle) - return false; } - let first = &attrs[0]; - self.format_missing_with_indent(source!(self, first.span).lo); + let rewrite = attrs.rewrite( + &self.get_context(), + Shape::indented(self.block_indent, self.config), + ); + let span = mk_sp(attrs[0].span.lo, attrs[attrs.len() - 1].span.hi); + self.push_rewrite(span, rewrite); - let rewrite = attrs - .rewrite( - &self.get_context(), - Shape::indented(self.block_indent, self.config), - ) - .unwrap(); - self.buffer.push_str(&rewrite); - let last = attrs.last().unwrap(); - self.last_pos = source!(self, last.span).hi; false } diff --git a/tests/source/attrib.rs b/tests/source/attrib.rs index beabed88510d..558153719475 100644 --- a/tests/source/attrib.rs +++ b/tests/source/attrib.rs @@ -1,6 +1,9 @@ // rustfmt-wrap_comments: true // Test attributes and doc comments are preserved. +#[invalid attribute] +fn foo() {} + /// Blah blah blah. /// Blah blah blah. /// Blah blah blah. diff --git a/tests/target/attrib.rs b/tests/target/attrib.rs index fc2dfc11ad97..542ed3676f0d 100644 --- a/tests/target/attrib.rs +++ b/tests/target/attrib.rs @@ -1,6 +1,9 @@ // rustfmt-wrap_comments: true // Test attributes and doc comments are preserved. +#[invalid attribute] +fn foo() {} + /// Blah blah blah. /// Blah blah blah. /// Blah blah blah.