Skip to content

Overhaul unnecessary_result_or_else #15801

Description

@ada4a

I originally wanted to suggest a new lint which would check the following pattern:

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

and would rewrite it as:

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

and same with Option::map_or_else.

But it turns out that there's already a pretty similar lint, unnecessary_result_map_or_else, which does the same for Results. Therefore, I'd instead suggest revamping that lint, by adding support both for Result::map_or and Option::map_or{,_else}.

That would of course require a name change, and I'd intuitively go with unnecessary_map_or_else. But there's already an unnecessary_map_or -- and I kind of feel like this new lint is more deserving of that name; maybe unnecessary_map_or could be renamed to something like map_or_for_partialeq?..

Also, currently unnecessary_result_map_or_else uses some bespoke algorithm to find out whether the "map closure" (as they call it) is an identity closure -- I think we should just switch to is_expr_untyped_identity_function instead. I guess this could be done separately from the rest.

@rustbot label C-enhancement

Metadata

Metadata

Assignees

Labels

C-enhancementCategory: Enhancement of lints, like adding more cases or adding help messages

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions