Skip to content

Commit c98a408

Browse files
committed
Minimize use of async token's span in expanded code
1 parent 2f5343c commit c98a408

File tree

3 files changed

+17
-23
lines changed

3 files changed

+17
-23
lines changed

src/bound.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use proc_macro2::{Ident, Span, TokenStream};
2-
use quote::quote_spanned;
2+
use quote::{quote, ToTokens};
33
use syn::punctuated::Punctuated;
44
use syn::{Token, TypeParamBound};
55

@@ -34,10 +34,12 @@ impl InferredBound {
3434
InferredBound::Sync => "Sync",
3535
}
3636
}
37+
}
3738

38-
pub fn spanned_path(&self, span: Span) -> TokenStream {
39-
let ident = Ident::new(self.as_str(), span);
40-
quote_spanned!(span=> ::core::marker::#ident)
39+
impl ToTokens for InferredBound {
40+
fn to_tokens(&self, tokens: &mut TokenStream) {
41+
let ident = Ident::new(self.as_str(), Span::call_site());
42+
quote!(::core::marker::#ident).to_tokens(tokens);
4143
}
4244
}
4345

src/expand.rs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ pub fn expand(input: &mut Item, is_local: bool) {
125125
fn lint_suppress_with_body() -> Attribute {
126126
parse_quote! {
127127
#[allow(
128-
unused_qualifications,
129128
clippy::async_yields_async,
130129
clippy::diverging_sub_expression,
131130
clippy::let_unit_value,
@@ -141,7 +140,6 @@ fn lint_suppress_with_body() -> Attribute {
141140
fn lint_suppress_without_body() -> Attribute {
142141
parse_quote! {
143142
#[allow(
144-
unused_qualifications,
145143
clippy::type_complexity,
146144
clippy::type_repetition_in_bounds
147145
)]
@@ -168,11 +166,10 @@ fn transform_sig(
168166
has_default: bool,
169167
is_local: bool,
170168
) {
171-
let default_span = sig.asyncness.take().unwrap().span;
172-
sig.fn_token.span = default_span;
169+
sig.fn_token.span = sig.asyncness.take().unwrap().span;
173170

174171
let (ret_arrow, ret) = match &sig.output {
175-
ReturnType::Default => (Token![->](default_span), quote_spanned!(default_span=> ())),
172+
ReturnType::Default => (Token![->](Span::call_site()), quote!(())),
176173
ReturnType::Type(arrow, ret) => (*arrow, quote!(#ret)),
177174
};
178175

@@ -234,9 +231,7 @@ fn transform_sig(
234231
.push(parse_quote_spanned!(elided.span()=> #elided: 'async_trait));
235232
}
236233

237-
sig.generics
238-
.params
239-
.push(parse_quote_spanned!(default_span=> 'async_trait));
234+
sig.generics.params.push(parse_quote!('async_trait));
240235

241236
if has_self {
242237
let bounds: &[InferredBound] = if is_local {
@@ -272,21 +267,17 @@ fn transform_sig(
272267
&[InferredBound::Send]
273268
};
274269

275-
let bounds = bounds.iter().filter_map(|bound| {
270+
let bounds = bounds.iter().filter(|bound| {
276271
let assume_bound = match context {
277272
Context::Trait { supertraits, .. } => !has_default || has_bound(supertraits, bound),
278273
Context::Impl { .. } => true,
279274
};
280-
if assume_bound {
281-
None
282-
} else {
283-
Some(bound.spanned_path(default_span))
284-
}
275+
!assume_bound
285276
});
286277

287278
where_clause_or_default(&mut sig.generics.where_clause)
288279
.predicates
289-
.push(parse_quote_spanned! {default_span=>
280+
.push(parse_quote! {
290281
Self: #(#bounds +)* 'async_trait
291282
});
292283
}
@@ -318,11 +309,11 @@ fn transform_sig(
318309
}
319310

320311
let bounds = if is_local {
321-
quote_spanned!(default_span=> 'async_trait)
312+
quote!('async_trait)
322313
} else {
323-
quote_spanned!(default_span=> ::core::marker::Send + 'async_trait)
314+
quote!(::core::marker::Send + 'async_trait)
324315
};
325-
sig.output = parse_quote_spanned! {default_span=>
316+
sig.output = parse_quote! {
326317
#ret_arrow ::core::pin::Pin<Box<
327318
dyn ::core::future::Future<Output = #ret> + #bounds
328319
>>
@@ -420,7 +411,7 @@ fn transform_block(context: Context, sig: &mut Signature, block: &mut Block) {
420411
quote!(#(#decls)* { #(#stmts)* })
421412
}
422413
} else {
423-
quote_spanned! {block.brace_token.span=>
414+
quote! {
424415
if let ::core::option::Option::Some(__ret) = ::core::option::Option::None::<#ret> {
425416
return __ret;
426417
}

tests/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,7 @@ pub mod issue158 {
13451345
fn f() {}
13461346

13471347
#[async_trait]
1348+
#[allow(unused_qualifications)]
13481349
pub trait Trait {
13491350
async fn f(&self) {
13501351
self::f();

0 commit comments

Comments
 (0)