From d533f904ab931a85224c7240bf8b4b06e0125705 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Tue, 25 Feb 2020 23:19:40 +0100 Subject: [PATCH] attributes: check if a skipped parameter exists (#600) This PR adds a check to the `#[instrument]` macro to emit a compiler error if the user tries to skip non-existing parameters. Fixes: #562 --- tracing-attributes/src/lib.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tracing-attributes/src/lib.rs b/tracing-attributes/src/lib.rs index 726118165f..0122972dc8 100644 --- a/tracing-attributes/src/lib.rs +++ b/tracing-attributes/src/lib.rs @@ -210,6 +210,19 @@ pub fn instrument(args: TokenStream, item: TokenStream) -> TokenStream { FnArg::Typed(PatType { pat, .. }) => param_names(*pat), FnArg::Receiver(_) => Box::new(iter::once(Ident::new("self", param.span()))), }) + .collect(); + + for skip in &skips { + if !param_names.contains(skip) { + return quote_spanned!(skip.span()=> + compile_error!("attempting to skip non-existent parameter") + ) + .into(); + } + } + + let param_names: Vec = param_names + .into_iter() .filter(|ident| !skips.contains(ident)) .collect();