Skip to content

Commit

Permalink
Use PathArguments::is_none more places where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Mar 22, 2023
1 parent 91ca7c0 commit 736d479
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 72 deletions.
43 changes: 19 additions & 24 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1681,30 +1681,25 @@ pub(crate) mod parsing {
fn path_or_macro_or_struct(input: ParseStream, allow_struct: AllowStruct) -> Result<Expr> {
let (qself, path) = path::parsing::qpath(input, true)?;

if qself.is_none() && input.peek(Token![!]) && !input.peek(Token![!=]) {
let mut contains_arguments = false;
for segment in &path.segments {
match segment.arguments {
PathArguments::None => {}
PathArguments::AngleBracketed(_) | PathArguments::Parenthesized(_) => {
contains_arguments = true;
}
}
}

if !contains_arguments {
let bang_token: Token![!] = input.parse()?;
let (delimiter, tokens) = mac::parse_delimiter(input)?;
return Ok(Expr::Macro(ExprMacro {
attrs: Vec::new(),
mac: Macro {
path,
bang_token,
delimiter,
tokens,
},
}));
}
if qself.is_none()
&& input.peek(Token![!])
&& !input.peek(Token![!=])
&& path
.segments
.iter()
.all(|segment| segment.arguments.is_none())
{
let bang_token: Token![!] = input.parse()?;
let (delimiter, tokens) = mac::parse_delimiter(input)?;
return Ok(Expr::Macro(ExprMacro {
attrs: Vec::new(),
mac: Macro {
path,
bang_token,
delimiter,
tokens,
},
}));
}

if allow_struct.0 && input.peek(token::Brace) {
Expand Down
43 changes: 19 additions & 24 deletions src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,30 +379,25 @@ pub(crate) mod parsing {
fn pat_path_or_macro_or_struct_or_range(input: ParseStream) -> Result<Pat> {
let (qself, path) = path::parsing::qpath(input, true)?;

if qself.is_none() && input.peek(Token![!]) && !input.peek(Token![!=]) {
let mut contains_arguments = false;
for segment in &path.segments {
match segment.arguments {
PathArguments::None => {}
PathArguments::AngleBracketed(_) | PathArguments::Parenthesized(_) => {
contains_arguments = true;
}
}
}

if !contains_arguments {
let bang_token: Token![!] = input.parse()?;
let (delimiter, tokens) = mac::parse_delimiter(input)?;
return Ok(Pat::Macro(ExprMacro {
attrs: Vec::new(),
mac: Macro {
path,
bang_token,
delimiter,
tokens,
},
}));
}
if qself.is_none()
&& input.peek(Token![!])
&& !input.peek(Token![!=])
&& path
.segments
.iter()
.all(|segment| segment.arguments.is_none())
{
let bang_token: Token![!] = input.parse()?;
let (delimiter, tokens) = mac::parse_delimiter(input)?;
return Ok(Pat::Macro(ExprMacro {
attrs: Vec::new(),
mac: Macro {
path,
bang_token,
delimiter,
tokens,
},
}));
}

if input.peek(token::Brace) {
Expand Down
43 changes: 19 additions & 24 deletions src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ pub(crate) mod parsing {
{
if let Type::Path(mut ty) = *group.elem {
let arguments = &mut ty.path.segments.last_mut().unwrap().arguments;
if let PathArguments::None = arguments {
if arguments.is_none() {
*arguments = PathArguments::AngleBracketed(input.parse()?);
Path::parse_rest(input, &mut ty.path, false)?;
return Ok(Type::Path(ty));
Expand Down Expand Up @@ -477,29 +477,24 @@ pub(crate) mod parsing {
return Ok(Type::Path(ty));
}

if input.peek(Token![!]) && !input.peek(Token![!=]) {
let mut contains_arguments = false;
for segment in &ty.path.segments {
match segment.arguments {
PathArguments::None => {}
PathArguments::AngleBracketed(_) | PathArguments::Parenthesized(_) => {
contains_arguments = true;
}
}
}

if !contains_arguments {
let bang_token: Token![!] = input.parse()?;
let (delimiter, tokens) = mac::parse_delimiter(input)?;
return Ok(Type::Macro(TypeMacro {
mac: Macro {
path: ty.path,
bang_token,
delimiter,
tokens,
},
}));
}
if input.peek(Token![!])
&& !input.peek(Token![!=])
&& ty
.path
.segments
.iter()
.all(|segment| segment.arguments.is_none())
{
let bang_token: Token![!] = input.parse()?;
let (delimiter, tokens) = mac::parse_delimiter(input)?;
return Ok(Type::Macro(TypeMacro {
mac: Macro {
path: ty.path,
bang_token,
delimiter,
tokens,
},
}));
}

if lifetimes.is_some() || allow_plus && input.peek(Token![+]) {
Expand Down

0 comments on commit 736d479

Please sign in to comment.