Fix a bug in attr::take_while_with_pred

Closes #2520.
This commit is contained in:
Seiichi Uchida 2018-03-09 09:29:08 +09:00
parent 0acc0a2989
commit 9344d2ca83

View file

@ -98,13 +98,16 @@ fn take_while_with_pred<'a, P>(
where where
P: Fn(&ast::Attribute) -> bool, P: Fn(&ast::Attribute) -> bool,
{ {
let mut last_index = 0; let mut len = 0;
let mut iter = attrs.iter().enumerate().peekable(); let mut iter = attrs.iter().peekable();
while let Some((i, attr)) = iter.next() {
if !pred(attr) { while let Some(attr) = iter.next() {
if pred(attr) {
len += 1;
} else {
break; break;
} }
if let Some(&(_, next_attr)) = iter.peek() { if let Some(next_attr) = iter.peek() {
// Extract comments between two attributes. // Extract comments between two attributes.
let span_between_attr = mk_sp(attr.span.hi(), next_attr.span.lo()); let span_between_attr = mk_sp(attr.span.hi(), next_attr.span.lo());
let snippet = context.snippet(span_between_attr); let snippet = context.snippet(span_between_attr);
@ -112,13 +115,9 @@ where
break; break;
} }
} }
last_index = i;
}
if last_index == 0 {
&[]
} else {
&attrs[..last_index + 1]
} }
&attrs[..len]
} }
/// Rewrite the same kind of attributes at the same time. This includes doc /// Rewrite the same kind of attributes at the same time. This includes doc