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

.map(|x| x.into()).map(Into::into) #10440

Closed
fenollp opened this issue Mar 3, 2023 · 2 comments
Closed

.map(|x| x.into()).map(Into::into) #10440

fenollp opened this issue Mar 3, 2023 · 2 comments
Labels
A-lint Area: New lints

Comments

@fenollp
Copy link

fenollp commented Mar 3, 2023

What it does

This lint replaces the introduction of a simple closure with the named function that is being used in said closure.

Lint Name

replace_mapped_closure_with_into

Category

complexity

Advantage

  • Reduces complexity of the map by introducing a common function name instead of a closure + identifier

Drawbacks

  • May be less readable for newcomers.
  • No performance drawback.

Example

impl IpResource {
    pub fn mac_address(&self) -> Option<MacAddr6> {
        self.mac_address.map(|x| x.into())
    }
}

Could be written as:

impl IpResource {
    pub fn mac_address(&self) -> Option<MacAddr6> {
        self.mac_address.map(Into::into)
    }
}
@fenollp fenollp added the A-lint Area: New lints label Mar 3, 2023
@Alexendoo
Copy link
Member

There's an existing lint that covers this: redundant_closure_for_method_calls

It is in the pedantic category though so it doesn't warn by default - #4101

@samueltardieu
Copy link
Contributor

Also note that, unlike in many functional languages, |x| f(x) cannot always be replaced by f (η-reduction) in Rust as f(x) might involve one or more reference and one dereference of x (such as &***x). Those dereferences/reference won't happen automatically when using only the function name, which wants the exact signature as declared.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

3 participants