Fixes #1184 where reordering import items could cause a panic.

Also extends an existing test to avoid regression.
This commit is contained in:
Fraser Hutchison
2016-11-03 04:21:47 +00:00
parent 8494d5f89c
commit f933d31115
3 changed files with 6 additions and 1 deletions
+4 -1
View File
@@ -158,7 +158,10 @@ pub fn format_imports(&mut self, use_items: &[ptr::P<ast::Item>]) {
// 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.
@@ -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;
@@ -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;