From f933d311157b90cdcb4f2bc670cea4844e5ea92e Mon Sep 17 00:00:00 2001 From: Fraser Hutchison Date: Thu, 3 Nov 2016 04:21:47 +0000 Subject: [PATCH 1/2] Fixes #1184 where reordering import items could cause a panic. Also extends an existing test to avoid regression. --- src/imports.rs | 5 ++++- tests/source/imports-reorder-lines-and-items.rs | 1 + tests/target/imports-reorder-lines-and-items.rs | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/imports.rs b/src/imports.rs index ed74d828a9cb..a01db7399732 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -158,7 +158,10 @@ pub fn format_imports(&mut self, use_items: &[ptr::P]) { // Find the location immediately before the first use item in the run. This must not lie // before the current `self.last_pos` let pos_before_first_use_item = use_items.first() - .map(|p_i| cmp::max(self.last_pos, p_i.span.lo)) + .map(|p_i| { + cmp::max(self.last_pos, + p_i.attrs.iter().map(|attr| attr.span.lo).min().unwrap_or(p_i.span.lo)) + }) .unwrap_or(self.last_pos); // Construct a list of pairs, each containing a `use` item and the start of span before // that `use` item. diff --git a/tests/source/imports-reorder-lines-and-items.rs b/tests/source/imports-reorder-lines-and-items.rs index b61f26771f60..3c71f9984feb 100644 --- a/tests/source/imports-reorder-lines-and-items.rs +++ b/tests/source/imports-reorder-lines-and-items.rs @@ -1,6 +1,7 @@ // rustfmt-reorder_imports: true // rustfmt-reorder_imported_names: true +/// This comment should stay with `use std::str;` use std::str; use std::cmp::{d, c, b, a}; use std::ddd::aaa; diff --git a/tests/target/imports-reorder-lines-and-items.rs b/tests/target/imports-reorder-lines-and-items.rs index fb2e0347aac5..f395710b186d 100644 --- a/tests/target/imports-reorder-lines-and-items.rs +++ b/tests/target/imports-reorder-lines-and-items.rs @@ -6,4 +6,5 @@ use std::ddd::aaa; // This comment should stay with `use std::ddd:bbb;` use std::ddd::bbb; +/// This comment should stay with `use std::str;` use std::str; From aef665aa21fa484fa5be3a779901fed9a95c5b89 Mon Sep 17 00:00:00 2001 From: Fraser Hutchison Date: Thu, 3 Nov 2016 04:22:16 +0000 Subject: [PATCH 2/2] Fixes a few doc typos. --- src/comment.rs | 4 ++-- src/visitor.rs | 2 +- tests/system.rs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/comment.rs b/src/comment.rs index 23d7dab3de97..cb7fdadd4a26 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -275,14 +275,14 @@ enum CharClassesStatus { LineComment, } -/// Distinguish between functionnal part of code and comments +/// Distinguish between functional part of code and comments #[derive(PartialEq, Eq, Debug, Clone, Copy)] pub enum CodeCharKind { Normal, Comment, } -/// Distinguish between functionnal part of code and comments, +/// Distinguish between functional part of code and comments, /// describing opening and closing of comments for ease when chunking /// code from tagged characters #[derive(PartialEq, Eq, Debug, Clone, Copy)] diff --git a/src/visitor.rs b/src/visitor.rs index f05fcefdfe8c..1f5d7c5414f8 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -179,7 +179,7 @@ fn visit_fn(&mut self, pub fn visit_item(&mut self, item: &ast::Item) { // This is where we bail out if there is a skip attribute. This is only // complex in the module case. It is complex because the module could be - // in a seperate file and there might be attributes in both files, but + // in a separate file and there might be attributes in both files, but // the AST lumps them all together. match item.node { ast::ItemKind::Mod(ref m) => { diff --git a/tests/system.rs b/tests/system.rs index 3534052a1732..a2a4c0eeedf8 100644 --- a/tests/system.rs +++ b/tests/system.rs @@ -33,7 +33,7 @@ fn get_path_string(dir_entry: io::Result) -> String { // Integration tests. The files in the tests/source are formatted and compared // to their equivalent in tests/target. The target file and config can be -// overriden by annotations in the source file. The input and output must match +// overridden by annotations in the source file. The input and output must match // exactly. // FIXME(#28) would be good to check for error messages and fail on them, or at // least report.