I am writing a proc macro attribute which is intended to be used as an attribute for #[test]
-functions. Briefly what happens is: depending on the position that I choose for my attribute in a function that is marked both #[test]
and #[should_panic]
, the compiler will complain that #[should_panic]
is an unused attribute, although the tests behave as they should.
Minimal Working Example
I have created a repo with an MWE here. It defines a proc macro attribute #[dummy]
, that parses a function and then spits it out unaltered like so:
#[proc_macro_attribute]
pub fn dummy(_args: TokenStream, input: TokenStream) -> TokenStream {
input
}
Now if I use this macro in tests from another crate, then the compiler will complain that #[should_panic]
is an unused attribute in these tests:
#[test]
#[dummy]
#[should_panic]
fn test3() {
assert_eq!(true,false);
}
#[test]
#[should_panic]
#[dummy]
fn test4() {
assert_eq!(true,false);
}
However, both tests pass when I run them, indicating that #[should_panic]
does what it is supposed to do.
Only if I stick my attribute at the top of the list of attributes will the compiler be happy:
#[dummy]
#[test]
#[should_panic]
fn test2() {
assert_eq!(true,false);
}
I experienced this behavior with both the compiler and clippy for toolchains 1.48.0 and 1.52.1. What is happening here?
EDIT: I simplified the proc macro by removing the dependency to the syn and quote crates. The behavior I am experiencing has nothing to do with the tokens being fed to the syn and quote crates.
EDIT 2: This topic reports the same problem. I did not find it during my first search. However, there seems to be no brilliant solution except to stick the custom attribute at the top. The linked topic is almost 2 years old. Is this still the case?