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

Suggest replacing pattern matching with map or map_err #8436

Open
lkts opened this issue Feb 15, 2022 · 1 comment
Open

Suggest replacing pattern matching with map or map_err #8436

lkts opened this issue Feb 15, 2022 · 1 comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages

Comments

@lkts
Copy link

lkts commented Feb 15, 2022

What it does

This is in some way an extension to redundant_pattern_matching lint. The lint flags pattern matching that can be trivially converted to map or map_err invocations.

Lint Name

extend redundant_pattern_matching or prefer_map_over_pattern_match

Category

style

Advantage

  • Code is shorter
  • Less indentation
  • Reduced boilerplate of mapping enum cases as is
  • In my experience this is a common pattern people new to Rust follow and it helps to discover the alternative

Drawbacks

Depending on the code base this lint can be very intrusive.

Example

let foo = match some_result_returning_fn() {
    Ok(val) => Ok(some_fn(val)),
    Err(e) => Err(e)
}

Could be written as:

let foo = some_result_returning_fn().map(some_fn);
let foo = match some_option_returning_fn() {
    Some(val) => Some(some_fn(val)),
    None => None
}

Could be written as:

let foo = some_option_returning_fn().map(some_fn);
let foo = match some_result_returning_fn() {
    Ok(val) => Ok(val),
    Err(e) => Err(some_fn(e))
}

Could be written as:

let foo = some_option_returning_fn().map_err(some_fn);
@lkts lkts added the A-lint Area: New lints label Feb 15, 2022
@giraffate
Copy link
Contributor

It would be better to improve the manual_map.

@giraffate giraffate added C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages and removed A-lint Area: New lints labels Feb 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages
Projects
None yet
Development

No branches or pull requests

2 participants