Skip to content

Commit

Permalink
Let dead_code lint work on #[instrumented functions
Browse files Browse the repository at this point in the history
Closes (partially?) #1366
  • Loading branch information
9999years committed Oct 17, 2024
1 parent bdbaf80 commit b1af999
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
38 changes: 27 additions & 11 deletions tracing-attributes/src/expand.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::iter;

use proc_macro2::TokenStream;
use quote::TokenStreamExt;
use quote::{quote, quote_spanned, ToTokens};
use syn::visit_mut::VisitMut;
use syn::{
Expand Down Expand Up @@ -29,6 +30,7 @@ pub(crate) fn gen_function<'a, B: ToTokens + 'a>(
inner_attrs,
vis,
sig,
brace_token,
block,
} = input;

Expand All @@ -44,9 +46,12 @@ pub(crate) fn gen_function<'a, B: ToTokens + 'a>(
syn::Generics {
params: gen_params,
where_clause,
..
lt_token,
gt_token,
},
..
fn_token,
paren_token,
variadic,
} = sig;

let warnings = args.warnings();
Expand Down Expand Up @@ -90,16 +95,27 @@ pub(crate) fn gen_function<'a, B: ToTokens + 'a>(
self_type,
);

quote!(
let mut result = quote!(
#(#outer_attrs) *
#vis #constness #asyncness #unsafety #abi fn #ident<#gen_params>(#params) #output
#where_clause
{
#(#inner_attrs) *
#warnings
#body
}
)
#vis #constness #asyncness #unsafety #abi #fn_token #ident
#lt_token #gen_params #gt_token
);

paren_token.surround(&mut result, |tokens| {
params.to_tokens(tokens);
variadic.to_tokens(tokens);
});

output.to_tokens(&mut result);
where_clause.to_tokens(&mut result);

brace_token.surround(&mut result, |tokens| {
tokens.append_all(inner_attrs);
warnings.to_tokens(tokens);
body.to_tokens(tokens);
});

result
}

/// Instrument a block
Expand Down
15 changes: 13 additions & 2 deletions tracing-attributes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@
)]

use proc_macro2::TokenStream;
use quote::TokenStreamExt;
use quote::{quote, ToTokens};
use syn::parse::{Parse, ParseStream};
use syn::token::Brace;
use syn::{Attribute, ItemFn, Signature, Visibility};

mod attr;
Expand Down Expand Up @@ -428,6 +430,7 @@ struct MaybeItemFn {
inner_attrs: Vec<Attribute>,
vis: Visibility,
sig: Signature,
brace_token: Brace,
block: TokenStream,
}

Expand All @@ -438,6 +441,7 @@ impl MaybeItemFn {
inner_attrs: &self.inner_attrs,
vis: &self.vis,
sig: &self.sig,
brace_token: &self.brace_token,
block: &self.block,
}
}
Expand All @@ -451,12 +455,15 @@ impl Parse for MaybeItemFn {
let vis: Visibility = input.parse()?;
let sig: Signature = input.parse()?;
let inner_attrs = input.call(Attribute::parse_inner)?;
let block: TokenStream = input.parse()?;
let block;
let brace_token = syn::braced!(block in input);
let block: TokenStream = block.call(|buffer| buffer.parse())?;
Ok(Self {
outer_attrs,
inner_attrs,
vis,
sig,
brace_token,
block,
})
}
Expand All @@ -474,12 +481,15 @@ impl From<ItemFn> for MaybeItemFn {
let (outer_attrs, inner_attrs) = attrs
.into_iter()
.partition(|attr| attr.style == syn::AttrStyle::Outer);
let mut block_tokens = TokenStream::new();
block_tokens.append_all(block.stmts);
Self {
outer_attrs,
inner_attrs,
vis,
sig,
block: block.to_token_stream(),
brace_token: block.brace_token,
block: block_tokens,
}
}
}
Expand All @@ -492,5 +502,6 @@ struct MaybeItemFnRef<'a, B: ToTokens> {
inner_attrs: &'a Vec<Attribute>,
vis: &'a Visibility,
sig: &'a Signature,
brace_token: &'a Brace,
block: &'a B,
}
10 changes: 10 additions & 0 deletions tracing-attributes/tests/dead_code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use tracing_attributes::instrument;

#[deny(unfulfilled_lint_expectations)]
#[expect(dead_code)]
#[instrument]
fn unused() {}

#[expect(dead_code)]
#[instrument]
async fn unused_async() {}

0 comments on commit b1af999

Please sign in to comment.