Unsure how to fix Clippy warning: Redundant Closure

For context I'm using Rocket and have a FromRequest guard here. The SessionId should be a struct, not a function.

warning: redundant closure
  --> src/routes/sessions.rs:15:18
   |
15 |             .map(|s| SessionId(s))
   |                  ^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `SessionId`
   |
#[derive(Debug)]
pub struct SessionId(pub String);

# the full function 
fn from_request(request: &'a Request<'r>) -> Outcome<SessionId, ()> {
        request
            .cookies()
            .get("session")
            .and_then(|cookie| cookie.value().parse().ok())
            .map(|s| SessionId(s))
            .or_forward(())
    }

A tuple struct like that is both a type and a function. See 1506-adt-kinds - The Rust RFC Book

So just .map(SessionId) is likely what you want.

3 Likes

Imo, I disagree with this clippy lint. It makes the code less readable and the reasoning given for why this is "bad" doesn't seem very strong. If it were me, I'd allow this specific lint at the crate level.

Ah ok :+1:
That clears up my clippy confusion.

I agree. This warning could be cleaned up a little for my use case too. But Iā€™m definitely gonna consider ignoring it

Also that's pretty cool, so map() is gonna pass an argument directly to the SessionId?

Is there a term for how the chaining value is used by .map()? I'm trying to google it but having trouble. For example if I wanted to log the value being sent to the map function, but prior to creating a new instance of SessionId

You could do logging like that in a .map closure, but you could also use inspect before your .map.

I'm not sure what you mean by "how the chaining value is used" though.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.