Skip to content

Commit

Permalink
Parenthesize other cases that require parens around impl Trait
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Jun 2, 2022
1 parent 3a4b7c2 commit bcdc758
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use proc_macro2::{Span, TokenStream};
use std::mem;
use syn::visit_mut::{self, VisitMut};
use syn::{
parse_quote_spanned, token, Expr, GenericArgument, Lifetime, Receiver, Type, TypeImplTrait,
TypeParen, TypeReference,
parse_quote_spanned, token, Expr, GenericArgument, Lifetime, Receiver, ReturnType, Type,
TypeBareFn, TypeImplTrait, TypeParen, TypePtr, TypeReference,
};

pub struct CollectLifetimes {
Expand Down Expand Up @@ -85,6 +85,18 @@ impl VisitMut for AddLifetimeToImplTrait {
visit_mut::visit_type_reference_mut(self, ty);
}

fn visit_type_ptr_mut(&mut self, ty: &mut TypePtr) {
parenthesize_impl_trait(&mut ty.elem, ty.star_token.span);
visit_mut::visit_type_ptr_mut(self, ty);
}

fn visit_type_bare_fn_mut(&mut self, ty: &mut TypeBareFn) {
if let ReturnType::Type(arrow, return_type) = &mut ty.output {
parenthesize_impl_trait(return_type, arrow.spans[0]);
}
visit_mut::visit_type_bare_fn_mut(self, ty);
}

fn visit_expr_mut(&mut self, _e: &mut Expr) {
// Do not recurse into impl Traits inside of an array length expression.
//
Expand Down
1 change: 1 addition & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1447,5 +1447,6 @@ pub mod issue204 {
#[async_trait]
pub trait Trait {
async fn f(arg: &impl Trait);
async fn g(arg: *const impl Trait);
}
}

0 comments on commit bcdc758

Please sign in to comment.