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

Okapi emits warnings when using a closure with no arguments #27

Closed
ThouCheese opened this issue May 19, 2020 · 4 comments
Closed

Okapi emits warnings when using a closure with no arguments #27

ThouCheese opened this issue May 19, 2020 · 4 comments

Comments

@ThouCheese
Copy link
Contributor

When writing a closure with no arguments, Okapi emits a warning on compilation.

#[openapi]
#[get("/?<id>")]
fn get_by_id(id: Option<i32>) -> String {
    let default = || 1;
    let id = id.unwrap_or_else(default);
    format!("got {}", id)
}

This gives the following result:

warning: unnecessary parentheses around type
  --> src/main.rs:16:1
   |
16 | #[get("/?<id>")]
   | ^^^^^^^^^^^^^^^^ help: remove these parentheses
   |
   = note: `#[warn(unused_parens)]` on by default

warning: 1 warning emitted

I've looked at the emitted code using cargo expand, but i couldn't figure out the cause of this warning.

@ralpha
Copy link
Collaborator

ralpha commented May 20, 2020

I was already in the code so took a look.
Apparently it is complaints about:

fn get_by_id(id: Option<(i32)>) -> String { ... }
                        ^^^^^ These

They are added by

fn preserve_span_information(input: TokenStream) -> TokenStream {

The function:

fn preserve_span_information(input: TokenStream) -> TokenStream {
    // Outputting the input unmodified would cause its span information to be
    // lost when being consumed by other macros. But parsing it in then quoting
    // it back out causes span information to be preserved.
    // See https://github.com/GREsau/okapi/issues/12
    // and https://github.com/rust-lang/rust/issues/43081
    let parsed_input: syn::Item = syn::parse(input).unwrap();

    // Nested generics cause span bugs - we can work around this by wrapping all
    // generic type parameters in parentheses.
    // https://github.com/rust-lang/rust/pull/48258
    let parsed_input = GenericTypeVisitor.fold_item(parsed_input);

    quote!(#parsed_input).into()
}

Looking at: #12 there are similar issues there (that you reported 😛 ).

I'm not familiar with that issue so will leave this to someone else (@GREsau 😉 ).

When commenting out this line I don't have the error anymore 😄

input = preserve_span_information(input);

maybe adding a strategic #[allow(unused_parens)] solves this. 🤫

(btw thanks for mentioning cargo expand did not know about it, could be useful in the future)

@GREsau
Copy link
Owner

GREsau commented May 24, 2020

Thanks for looking into this - yep, I think #[allow(unused_parens)] is going to be the simplest fix for this

@GREsau
Copy link
Owner

GREsau commented May 24, 2020

Actually now that I've taken a closer look, I think the best fix is to just remove the fixes that I added for #12 (which I'm very happy about, since that was a horrible hack!)

When I remove preserve_span_information, it not only fixes this issue, but I also can't reproduce the original issues reported in #12 - I can only assume that they've since been fixed in rustc somehow?

GREsau added a commit that referenced this issue May 24, 2020
I don't believe this is necessary in newer nightlies.
It also causes its own warnings (#27).
@GREsau
Copy link
Owner

GREsau commented May 24, 2020

This should be now fixed in v0.5.0 🙂

@GREsau GREsau closed this as completed May 24, 2020
mautamu pushed a commit to mautamu/okapi that referenced this issue Apr 23, 2021
I don't believe this is necessary in newer nightlies.
It also causes its own warnings (GREsau#27).
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

3 participants