diff --git a/src/imports.rs b/src/imports.rs index ed74d828a9c..a01db739973 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -158,7 +158,10 @@ impl<'a> FmtVisitor<'a> { // 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 b61f26771f6..3c71f9984fe 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 fb2e0347aac..f395710b186 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::{a, b, c as g, d as p}; 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;