Deprecate redundant lint option_map_or_err_ok and take manual_ok_or out of pedantic (#14027)

While extending the `option_map_or_err_ok` lint (warn by default,
"style") to recognize η-expanded forms of `Ok`, as in

```rust
    // Should suggest `opt.ok_or("foobar")`
   let _ = opt.map_or(Err("foobar"), |x| Ok(x));
```

I discovered that the `manual_ok_or` lint (allow by default, "pedantic")
already covered exactly the cases handled by `option_map_or_err_ok`,
including the one I was adding. Apparently, `option_map_or_err_ok` was
added without realizing that the lint already existed under the
`manual_ok_or` name. As a matter of fact, artifacts of this second lint
were even present in the first lint `stderr` file and went unnoticed for
more than a year.

This PR:
- deprecates `option_map_or_err_ok` with a message saying to use
`manual_ok_or`
- moves `manual_ok_or` from "pedantic" to "style" (the category in which
`option_map_or_err_ok` was)

In addition, I think that this lint, which is short, machine applicable,
and leads to shorter and clearer code with less arguments (`Ok`
disappears) and the removal of one level of call (`Err(x)` is replaced
by `x`), is a reason by itself to be in "style".

changelog: [`option_map_or_err_ok` and `manual_ok_or`]: move
`manual_ok_or` from "pedantic" to "style", and deprecate the redundant
style lint `option_map_or_err_ok`.
This commit is contained in:
dswij 2025-02-07 17:34:21 +00:00 committed by GitHub
commit 4e5d00a0a7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 12 additions and 107 deletions

View file

@ -15,5 +15,6 @@
#![warn(clippy::regex_macro)] //~ ERROR: lint `clippy::regex_macro`
#![warn(clippy::pub_enum_variant_names)] //~ ERROR: lint `clippy::pub_enum_variant_names`
#![warn(clippy::wrong_pub_self_convention)] //~ ERROR: lint `clippy::wrong_pub_self_convention`
#![warn(clippy::option_map_or_err_ok)] //~ ERROR: lint `clippy::option_map_or_err_ok`
fn main() {}

View file

@ -79,5 +79,11 @@ error: lint `clippy::wrong_pub_self_convention` has been removed: `clippy::wrong
LL | #![warn(clippy::wrong_pub_self_convention)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 13 previous errors
error: lint `clippy::option_map_or_err_ok` has been removed: `clippy::manual_ok_or` covers this case
--> tests/ui/deprecated.rs:18:9
|
LL | #![warn(clippy::option_map_or_err_ok)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 14 previous errors

View file

@ -13,15 +13,6 @@ error: this pattern reimplements `Option::ok_or`
LL | foo.map_or(Err("error"), Ok);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `foo.ok_or("error")`
error: called `map_or(Err(_), Ok)` on an `Option` value
--> tests/ui/manual_ok_or.rs:14:5
|
LL | foo.map_or(Err("error"), Ok);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `ok_or`: `foo.ok_or("error")`
|
= note: `-D clippy::option-map-or-err-ok` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::option_map_or_err_ok)]`
error: this pattern reimplements `Option::ok_or`
--> tests/ui/manual_ok_or.rs:17:5
|
@ -47,5 +38,5 @@ LL + "{}{}{}{}{}{}{}",
LL ~ "Alice", "Bob", "Sarah", "Marc", "Sandra", "Eric", "Jenifer"));
|
error: aborting due to 5 previous errors
error: aborting due to 4 previous errors

View file

@ -1,7 +0,0 @@
#![warn(clippy::option_map_or_err_ok)]
fn main() {
let x = Some("a");
let _ = x.ok_or("a");
//~^ ERROR: called `map_or(Err(_), Ok)` on an `Option` value
}

View file

@ -1,7 +0,0 @@
#![warn(clippy::option_map_or_err_ok)]
fn main() {
let x = Some("a");
let _ = x.map_or(Err("a"), Ok);
//~^ ERROR: called `map_or(Err(_), Ok)` on an `Option` value
}

View file

@ -1,11 +0,0 @@
error: called `map_or(Err(_), Ok)` on an `Option` value
--> tests/ui/option_map_or_err_ok.rs:5:13
|
LL | let _ = x.map_or(Err("a"), Ok);
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider using `ok_or`: `x.ok_or("a")`
|
= note: `-D clippy::option-map-or-err-ok` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::option_map_or_err_ok)]`
error: aborting due to 1 previous error