-
Notifications
You must be signed in to change notification settings - Fork 74
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
Should warn or error on unregistered handlers #114
Comments
When I suggested filing this, I thought fixing it would be pretty straightforward. As it turns out, it's not straightforward to cause normal unused warnings from items generated from proc macros. I asked in discord so perhaps I'll track down someone who knows: https://discord.com/channels/273534239310479360/512792629516173323/849808892103229521 @dtolnay if you can hear me, I'd love your input: with in a proc_macro we generate a token stream that includes this: const FOO: FooType = FooType {}; We'd ideally like it to be a warning if the program never uses |
Seems like rustc throws out the warning if it's spanned with Tested with: // src/lib.rs
use proc_macro::TokenStream;
use proc_macro2::{Ident, Punct, Spacing};
use quote::{quote, quote_spanned};
use syn::{parse_macro_input, ItemFn};
#[proc_macro_attribute]
pub fn ahl0(_args: TokenStream, _input: TokenStream) -> TokenStream {
TokenStream::from(quote! {
const FOO: () = ();
})
}
#[proc_macro_attribute]
pub fn ahl1(_args: TokenStream, input: TokenStream) -> TokenStream {
let item = parse_macro_input!(input as ItemFn);
let span = item.sig.ident.span();
TokenStream::from(quote_spanned! {span=>
const FOO: () = ();
})
}
#[proc_macro_attribute]
pub fn ahl2(_args: TokenStream, input: TokenStream) -> TokenStream {
let mut iter = input.into_iter();
let begin = iter.next().unwrap().span();
let end = iter.last().unwrap().span();
let const_token = Ident::new("const", begin.into());
let mut semi_token = Punct::new(';', Spacing::Alone);
semi_token.set_span(end.into());
TokenStream::from(quote! {
#const_token FOO: () = () #semi_token
})
} // src/main.rs
pub mod f {
#[repro::ahl0]
pub fn test() {}
}
pub mod g {
#[repro::ahl1]
pub fn test() {}
}
pub mod h {
#[repro::ahl2]
pub fn test() {}
}
fn main() {} warning: constant is never used: `FOO`
--> src/main.rs:8:12
|
8 | pub fn test() {}
| ^^^^
warning: constant is never used: `FOO`
--> src/main.rs:13:5
|
13 | pub fn test() {}
| ^^^^^^^^^^^^^^^^ |
If you write an
endpoint
function and forget toapi.register
it, the server will 404 on those routes, but it's not easy to figure out the problem. It would be helpful if there was a compiler error or warning for unregistered route handlers.The text was updated successfully, but these errors were encountered: