Change empty replace range condition.

The new condition is equivalent in practice, but it's much more obvious
that it would result in an empty range, because the condition lines up
with the contents of the iterator.
This commit is contained in:
Nicholas Nethercote 2024-07-10 14:41:39 +10:00
parent f5527949f2
commit 8a390bae06

View File

@ -295,21 +295,22 @@ pub fn collect_tokens_trailing_token<R: HasAttrs + HasTokens>(
let num_calls = end_pos - start_pos; let num_calls = end_pos - start_pos;
// If we have no attributes, then we will never need to // This is hot enough for `deep-vector` that checking the conditions for an empty iterator
// use any replace ranges. // is measurably faster than actually executing the iterator.
let replace_ranges: Box<[ReplaceRange]> = if ret.attrs().is_empty() && !self.capture_cfg { let replace_ranges: Box<[ReplaceRange]> =
Box::new([]) if replace_ranges_start == replace_ranges_end && inner_attr_replace_ranges.is_empty() {
} else { Box::new([])
// Grab any replace ranges that occur *inside* the current AST node. } else {
// We will perform the actual replacement when we convert the `LazyAttrTokenStream` // Grab any replace ranges that occur *inside* the current AST node.
// to an `AttrTokenStream`. // We will perform the actual replacement when we convert the `LazyAttrTokenStream`
self.capture_state.replace_ranges[replace_ranges_start..replace_ranges_end] // to an `AttrTokenStream`.
.iter() self.capture_state.replace_ranges[replace_ranges_start..replace_ranges_end]
.cloned() .iter()
.chain(inner_attr_replace_ranges.iter().cloned()) .cloned()
.map(|(range, data)| ((range.start - start_pos)..(range.end - start_pos), data)) .chain(inner_attr_replace_ranges.iter().cloned())
.collect() .map(|(range, data)| ((range.start - start_pos)..(range.end - start_pos), data))
}; .collect()
};
let tokens = LazyAttrTokenStream::new(LazyAttrTokenStreamImpl { let tokens = LazyAttrTokenStream::new(LazyAttrTokenStreamImpl {
start_token, start_token,