rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Added new lint for `map_or(..., identity)`

Open teofr opened this issue 2 months ago • 1 comments

Introduce a new lint for mapping the identity function with map_or over Option and Result.

Lints:

fn foo(opt: Option<i32>, default: i32) -> i32 {
    opt.map_or(default, |o| o)
}

Into

fn foo(opt: Option<i32>, default: i32) -> i32 {
    opt.unwrap_or(default)
}

Fixes rust-lang/rust-clippy#15801

We had some discussions with @ada4a about naming on issue rust-lang/rust-clippy#15801 , a summary here.

There's a question about naming for this lint, there's already unnecessary_option_map_or_else and unnecessary_result_map_or_else, but I think having two lints doesn't make sense in this case. unnecessary_map_or could be a reasonable name for this new lint, but it's already taken

changelog: [map_or_identity]: new lint

teofr avatar Nov 08 '25 10:11 teofr

r? @samueltardieu

rustbot has assigned @samueltardieu. They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

rustbot avatar Nov 28 '25 07:11 rustbot