Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let dead_code lint work on #[instrument]ed functions #3108

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/examples/tower-load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ async fn main() -> Result<(), Err> {
let svc = Server::bind(&addr).serve(svc);
let admin = Server::bind(&admin_addr).serve(admin);

#[allow(clippy::incompatible_msrv)]
let res = try_join!(
tokio::spawn(load_gen(addr)),
tokio::spawn(load_gen(addr)),
Expand Down
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,
}
5 changes: 5 additions & 0 deletions tracing-attributes/tests/async_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ async fn test_ret_impl_trait_err(n: i32) -> Result<impl Iterator<Item = i32>, &'
}

#[instrument]
#[allow(dead_code)]
async fn test_async_fn_empty() {}

#[instrument]
#[allow(dead_code)]
async unsafe fn test_async_unsafe_fn_empty() {}

// Reproduces a compile error when an instrumented function body contains inner
// attributes (https://github.com/tokio-rs/tracing/issues/2294).
#[deny(unused_variables)]
#[allow(dead_code, clippy::mixed_attributes_style)]
#[instrument]
async fn repro_async_2294() {
#![allow(unused_variables)]
Expand All @@ -50,6 +53,7 @@ async fn repro_async_2294() {
// with the rustfmt-generated formatting, the lint will not be triggered!
#[rustfmt::skip]
#[deny(clippy::suspicious_else_formatting)]
#[allow(dead_code)]
async fn repro_1613(var: bool) {
println!(
"{}",
Expand All @@ -61,6 +65,7 @@ async fn repro_1613(var: bool) {
// and https://github.com/rust-lang/rust-clippy/issues/7760
#[instrument]
#[deny(clippy::suspicious_else_formatting)]
#[allow(dead_code)]
async fn repro_1613_2() {
// hello world
// else
Expand Down
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() {}
1 change: 1 addition & 0 deletions tracing-attributes/tests/err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ fn err() -> Result<u8, TryFromIntError> {
}

#[instrument(err)]
#[allow(dead_code)]
fn err_suspicious_else() -> Result<u8, TryFromIntError> {
{}
u8::try_from(1234)
Expand Down
1 change: 1 addition & 0 deletions tracing-attributes/tests/instrument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use tracing_mock::*;
// Reproduces a compile error when an instrumented function body contains inner
// attributes (https://github.com/tokio-rs/tracing/issues/2294).
#[deny(unused_variables)]
#[allow(dead_code, clippy::mixed_attributes_style)]
#[instrument]
fn repro_2294() {
#![allow(unused_variables)]
Expand Down
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)
Loading