Skip to content

Commit

Permalink
Allow unreachable code in #[debug_handler]
Browse files Browse the repository at this point in the history
As reported in tokio-rs#2004, the usage of `#[debug_handler]` conflicts with
`#![deny(unreachable_code)]` since the macro itself generates
unreachable code by design.

This change:
* Adds allow rules to the generated code to prevent this

Closes tokio-rs#2004
  • Loading branch information
alexander-jackson committed May 19, 2023
1 parent 59d6a7b commit 51ec881
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions axum-macros/src/debug_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ fn check_inputs_impls_from_request(item_fn: &ItemFn, state_ty: Type) -> TokenStr

quote_spanned! {span=>
#[allow(warnings)]
#[allow(unreachable_code)]
#[doc(hidden)]
fn #check_fn #check_fn_generics()
where
Expand All @@ -272,6 +273,7 @@ fn check_inputs_impls_from_request(item_fn: &ItemFn, state_ty: Type) -> TokenStr
// we have to call the function to actually trigger a compile error
// since the function is generic, just defining it is not enough
#[allow(warnings)]
#[allow(unreachable_code)]
#[doc(hidden)]
fn #call_check_fn()
{
Expand Down Expand Up @@ -415,6 +417,7 @@ fn check_output_impls_into_response(item_fn: &ItemFn) -> TokenStream {
let make = if item_fn.sig.asyncness.is_some() {
quote_spanned! {span=>
#[allow(warnings)]
#[allow(unreachable_code)]
#[doc(hidden)]
async fn #make_value_name() -> #ty {
#declare_inputs
Expand All @@ -424,6 +427,7 @@ fn check_output_impls_into_response(item_fn: &ItemFn) -> TokenStream {
} else {
quote_spanned! {span=>
#[allow(warnings)]
#[allow(unreachable_code)]
#[doc(hidden)]
fn #make_value_name() -> #ty {
#declare_inputs
Expand All @@ -439,6 +443,7 @@ fn check_output_impls_into_response(item_fn: &ItemFn) -> TokenStream {
#make

#[allow(warnings)]
#[allow(unreachable_code)]
#[doc(hidden)]
async fn #name() {
let value = #receiver #make_value_name().await;
Expand All @@ -451,6 +456,7 @@ fn check_output_impls_into_response(item_fn: &ItemFn) -> TokenStream {
} else {
quote_spanned! {span=>
#[allow(warnings)]
#[allow(unreachable_code)]
#[doc(hidden)]
async fn #name() {
#make
Expand Down Expand Up @@ -501,6 +507,7 @@ fn check_future_send(item_fn: &ItemFn) -> TokenStream {
if let Some(receiver) = self_receiver(item_fn) {
quote! {
#[allow(warnings)]
#[allow(unreachable_code)]
#[doc(hidden)]
fn #name() {
let future = #receiver #handler_name(#(#args),*);
Expand All @@ -510,6 +517,7 @@ fn check_future_send(item_fn: &ItemFn) -> TokenStream {
} else {
quote! {
#[allow(warnings)]
#[allow(unreachable_code)]
#[doc(hidden)]
fn #name() {
#item_fn
Expand Down
8 changes: 8 additions & 0 deletions axum-macros/tests/debug_handler/pass/deny_unreachable_code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![deny(unreachable_code)]

use axum::extract::Path;

#[axum_macros::debug_handler]
async fn handler(Path(_): Path<String>) {}

fn main() {}

0 comments on commit 51ec881

Please sign in to comment.