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 0928c37
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 28 deletions.
49 changes: 35 additions & 14 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 All @@ -65,9 +70,14 @@ pub(crate) fn gen_function<'a, B: ToTokens + 'a>(
// exactly that way for it to do its magic.
let fake_return_edge = quote_spanned! {return_span=>
#[allow(
unknown_lints, unreachable_code, clippy::diverging_sub_expression,
clippy::let_unit_value, clippy::unreachable, clippy::let_with_type_underscore,
clippy::empty_loop
unknown_lints,
unreachable_code,
clippy::diverging_sub_expression,
clippy::empty_loop,
clippy::let_unit_value,
clippy::let_with_type_underscore,
clippy::needless_return,
clippy::unreachable
)]
if false {
let __tracing_attr_fake_return: #return_type = loop {};
Expand All @@ -90,16 +100,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() {}
27 changes: 15 additions & 12 deletions tracing-attributes/tests/ui/async_instrument.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ note: return type inferred to be `String` here
| ^^^^^^

error[E0277]: `(&str,)` doesn't implement `std::fmt::Display`
--> tests/ui/async_instrument.rs:14:1
--> tests/ui/async_instrument.rs:15:57
|
14 | #[tracing::instrument]
| ^^^^^^^^^^^^^^^^^^^^^^ `(&str,)` cannot be formatted with the default formatter
15 | async fn opaque_unsatisfied() -> impl std::fmt::Display {
| _________________________________________________________^
16 | | ("",)
17 | | }
| |_^ `(&str,)` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `(&str,)`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= note: this error originates in the attribute macro `tracing::instrument` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: `(&str,)` doesn't implement `std::fmt::Display`
--> tests/ui/async_instrument.rs:15:34
Expand Down Expand Up @@ -88,11 +90,12 @@ note: return type inferred to be `String` here
| ^^^^^^

error[E0308]: mismatched types
--> tests/ui/async_instrument.rs:42:35
|
42 | async fn extra_semicolon() -> i32 {
| ___________________________________^
43 | | 1;
| | - help: remove this semicolon to return this value
44 | | }
| |_^ expected `i32`, found `()`
--> tests/ui/async_instrument.rs:41:1
|
41 | #[tracing::instrument]
| ^^^^^^^^^^^^^^^^^^^^^^ expected `i32`, found `()`
42 | async fn extra_semicolon() -> i32 {
43 | 1;
| - help: remove this semicolon to return this value
|
= note: this error originates in the attribute macro `tracing::instrument` (in Nightly builds, run with -Z macro-backtrace for more info)

0 comments on commit 0928c37

Please sign in to comment.