Invert early exit conditions in collect_tokens_trailing_token.

This has been bugging me for a while. I find complex "if any of these
are true" conditions easier to think about than complex "if all of these
are true" conditions, because you can stop as soon as one is true.
This commit is contained in:
Nicholas Nethercote 2024-07-26 08:42:43 +10:00
parent 6106b05b27
commit caee195bdd

View File

@ -199,20 +199,20 @@ pub fn collect_tokens_trailing_token<R: HasAttrs + HasTokens>(
force_collect: ForceCollect, force_collect: ForceCollect,
f: impl FnOnce(&mut Self, ast::AttrVec) -> PResult<'a, (R, bool)>, f: impl FnOnce(&mut Self, ast::AttrVec) -> PResult<'a, (R, bool)>,
) -> PResult<'a, R> { ) -> PResult<'a, R> {
// Skip collection when nothing could observe the collected tokens, i.e. // We must collect if anything could observe the collected tokens, i.e.
// all of the following conditions hold. // if any of the following conditions hold.
// - We are not force collecting tokens (because force collection // - We are force collecting tokens (because force collection requires
// requires tokens by definition). // tokens by definition).
if matches!(force_collect, ForceCollect::No) let needs_collection = matches!(force_collect, ForceCollect::Yes)
// - None of our outer attributes require tokens. // - Any of our outer attributes require tokens.
&& attrs.is_complete() || !attrs.is_complete()
// - Our target doesn't support custom inner attributes (custom // - Our target supports custom inner attributes (custom
// inner attribute invocation might require token capturing). // inner attribute invocation might require token capturing).
&& !R::SUPPORTS_CUSTOM_INNER_ATTRS || R::SUPPORTS_CUSTOM_INNER_ATTRS
// - We are not in `capture_cfg` mode (which requires tokens if // - We are in `capture_cfg` mode (which requires tokens if
// the parsed node has `#[cfg]` or `#[cfg_attr]` attributes). // the parsed node has `#[cfg]` or `#[cfg_attr]` attributes).
&& !self.capture_cfg || self.capture_cfg;
{ if !needs_collection {
return Ok(f(self, attrs.attrs)?.0); return Ok(f(self, attrs.attrs)?.0);
} }
@ -250,28 +250,28 @@ pub fn collect_tokens_trailing_token<R: HasAttrs + HasTokens>(
return Ok(ret); return Ok(ret);
} }
// This is similar to the "skip collection" check at the start of this // This is similar to the `needs_collection` check at the start of this
// function, but now that we've parsed an AST node we have more // function, but now that we've parsed an AST node we have complete
// information available. (If we return early here that means the // information available. (If we return early here that means the
// setup, such as cloning the token cursor, was unnecessary. That's // setup, such as cloning the token cursor, was unnecessary. That's
// hard to avoid.) // hard to avoid.)
// //
// Skip collection when nothing could observe the collected tokens, i.e. // We must collect if anything could observe the collected tokens, i.e.
// all of the following conditions hold. // if any of the following conditions hold.
// - We are not force collecting tokens. // - We are force collecting tokens.
if matches!(force_collect, ForceCollect::No) let needs_collection = matches!(force_collect, ForceCollect::Yes)
// - None of our outer *or* inner attributes require tokens. // - Any of our outer *or* inner attributes require tokens.
// (`attrs` was just outer attributes, but `ret.attrs()` is outer // (`attrs` was just outer attributes, but `ret.attrs()` is outer
// and inner attributes. That makes this check more precise than // and inner attributes. So this check is more precise than the
// `attrs.is_complete()` at the start of the function, and we can // earlier `attrs.is_complete()` check, and we don't need to
// skip the subsequent check on `R::SUPPORTS_CUSTOM_INNER_ATTRS`. // check `R::SUPPORTS_CUSTOM_INNER_ATTRS`.)
&& crate::parser::attr::is_complete(ret.attrs()) || !crate::parser::attr::is_complete(ret.attrs())
// - We are not in `capture_cfg` mode, or we are but there are no // - We are in `capture_cfg` mode and there are `#[cfg]` or
// `#[cfg]` or `#[cfg_attr]` attributes. (During normal // `#[cfg_attr]` attributes. (During normal non-`capture_cfg`
// non-`capture_cfg` parsing, we don't need any special capturing // parsing, we don't need any special capturing for those
// for those attributes, because they're builtin.) // attributes, because they're builtin.)
&& (!self.capture_cfg || !has_cfg_or_cfg_attr(ret.attrs())) || (self.capture_cfg && has_cfg_or_cfg_attr(ret.attrs()));
{ if !needs_collection {
return Ok(ret); return Ok(ret);
} }