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

Clippy unused_unit warning in generated code #2532

Closed
sjmurdoch opened this issue May 2, 2023 · 2 comments
Closed

Clippy unused_unit warning in generated code #2532

sjmurdoch opened this issue May 2, 2023 · 2 comments

Comments

@sjmurdoch
Copy link
Contributor

Description

In some circumstances, a route handler will introduce a Clippy warning. Because the warning is in generated code, this is hard to suppress selectively.

To Reproduce

#[get("/form?foo=bar")]
async fn analyse_example_test() -> Option<Template> {
    None
}

creates the error

   --> src/main.rs:568:1
    |
568 | #[get("/form?foo=bar")]
    | ^^^^^^^^^^^^^^^^^^^^^^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unused_unit
    = note: `#[warn(clippy::unused_unit)]` on by default
    = note: this warning originates in the attribute macro `get` (in Nightly builds, run with -Z macro-backtrace for more info)

with the generated code

// Recursive expansion of get macro
// =================================

async fn analyse_example_test() -> Option<Template> {
    None
}
#[doc(hidden)]
#[allow(non_camel_case_types)]
#[doc = r" Rocket code generated proxy structure."]
struct analyse_example_test {}

#[doc = r" Rocket code generated proxy static conversion implementations."]
impl analyse_example_test {
    #[allow(non_snake_case, unreachable_patterns, unreachable_code)]
    fn into_info(self) -> ::rocket::route::StaticInfo {
        fn monomorphized_function<'__r>(
            __req: &'__r ::rocket::request::Request<'_>,
            __data: ::rocket::data::Data<'__r>,
        ) -> ::rocket::route::BoxFuture<'__r> {
            ::std::boxed::Box::pin(async move {
                let () = {
                    let mut __e = ::rocket::form::prelude::Errors::new();
                    for _f in __req.query_fields() {
                        let _raw = (_f.name.source().as_str(), _f.value);
                        let _key = _f.name.key_lossy().as_str();
                        match (_raw, _key) {
                            (("foo", "bar"), _) => {}
                            _ => {}
                        }
                    }
                    if !__e.is_empty() {
                        ::rocket::log::warn_!("Query string failed to match route declaration.");
                        for _err in __e {
                            ::rocket::log::warn_!("{}", _err);
                        }
                        return ::rocket::outcome::Outcome::Forward(__data);
                    }
                    ()
                };
                let ___responder = analyse_example_test().await;
                ::rocket::route::Outcome::from(__req, ___responder)
            })
        }
        ::rocket::route::StaticInfo {
            name: stringify!(analyse_example_test),
            method: ::rocket::http::Method::Get,
            uri: "/form?foo=bar",
            handler: monomorphized_function,
            format: ::std::option::Option::None,
            rank: ::std::option::Option::None,
            sentinels: ::std::vec![
                ::rocket::sentinel::resolve!(Option<Template>),
                ::rocket::sentinel::resolve!(Template, Option<Template>)
            ],
        }
    }
    #[doc(hidden)]
    pub fn into_route(self) -> ::rocket::Route {
        self.into_info().into()
    }
}
#[doc = r" Rocket code generated wrapping URI macro."]
#[doc(hidden)]
#[macro_export]
#[doc = r" Rocket generated URI macro."]
macro_rules! rocket_uri_macro_analyse_example_test_1196246365344693276 {
  ($($token:tt)*) => {
    {
      rocket::rocket_internal_uri!("/form?foo=bar",(), $($token)*)
    }
  };
}
#[doc(hidden)]
pub use rocket_uri_macro_analyse_example_test_1196246365344693276 as rocket_uri_macro_analyse_example_test;

Expected Behavior

No warning

Environment:

  • rustc 1.69.0 (84c898d65 2023-04-16)
  • OS Distribution and Kernel: Debian 11.6 under WSL2 (Linux 6bd635bc85e8 5.10.102.1-microsoft-standard-WSL2 #⁠1 SMP Wed Mar 2 00:30:59 UTC 2022 x86_64 GNU/Linux)
  • Rocket Version: 0.5.0-rc.3
@sjmurdoch sjmurdoch added the triage A bug report being investigated label May 2, 2023
@SergioBenitez SergioBenitez removed the triage A bug report being investigated label May 4, 2023
@SergioBenitez
Copy link
Member

For query strings, we generate using something like the following:

let (#(#ident),*) = {
    // query parsing code ...
    (#(#ident.unwrap()),*)
};

Where ident is a vector of identifiers, one for each dynamic parameter in the query string. If there are no dynamic parameters, such as in your example, the vector is empty, and we generate the following:

let () = {
    // query parsing code ...
    ()
};

This is what clippy complains about, as this is equivalent to:

{
    // query parsing code ...
};

Which resolves the clippy warning.

In general, our stance is that clippy should not emit warnings for proc-macro generate code. As you can see, this warning is emitted because our code generation is generic to the number of identifiers. "Fixing" this in Rocket would mean creating a special case for when there are no dynamic parameters, which seems completely unnecessary. I would suggest filing an issue with Clippy.

In any case, I'll investigate silencing clippy for Rocket's codegen altogether.

@SergioBenitez SergioBenitez closed this as not planned Won't fix, can't repro, duplicate, stale May 4, 2023
@SergioBenitez
Copy link
Member

For posterity, c1ead84 silences this warning, and any like it in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants