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 Option::is_some_and(...) instead of map_or(false, ...) and similar for Results #12958

Closed
sdroege opened this issue Jun 19, 2024 · 2 comments
Labels
A-lint Area: New lints

Comments

@sdroege
Copy link

sdroege commented Jun 19, 2024

What it does

Given the following code

// 1
if some_option.map_or(false, |x| some_predicate(x)) {
    // do something
}
// 2
if some_option.map(|x| some_predicate(x)).unwrap_or(false) { // unwrap_or_default() etc
    // do something
}
// 3
if some_result.map_or(false, |x| some_predicate(x)) {
    // do something
}
// 4
if some_result.map(|x| some_predicate(x)).unwrap_or(false) { // unwrap_or_default() etc
    // do something
}

it would suggest

// 1
if some_option.is_some_and(|x| some_predicate(x)) {
    // do something
}
// 2
if some_option.is_some_and(|x| some_predicate(x)) {
    // do something
}
// 3
if some_result.is_ok_and(|x| some_predicate(x)) {
    // do something
}
// 4
if some_result.is_ok_and(|x| some_predicate(x)) {
    // do something
}

Not sure if there's something meaningful around Result::is_err_and() that can be transformed in the same way

Advantage

Shorter and more descriptive

Drawbacks

Requires MSRV 1.70

Example

See example in initial description

@sdroege sdroege added the A-lint Area: New lints label Jun 19, 2024
@y21
Copy link
Member

y21 commented Jun 19, 2024

Case 1 and 3 looks like #11848 (which has an implementation PR open). Case 2 and 4 should already be covered by manual_is_variant_and.

@sdroege
Copy link
Author

sdroege commented Jun 19, 2024

Hrm, did I typo while searching for these. Sorry for the noise!

I don't think these should be pedantic though and could be enabled by default as well.

@sdroege sdroege closed this as completed Jun 19, 2024
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

2 participants