From 06221e653ca57a51b94a794cd99c49c8cba68ff9 Mon Sep 17 00:00:00 2001 From: Samuel Tardieu Date: Sat, 18 Jan 2025 17:14:33 +0100 Subject: Deprecate the `option_map_or_err_ok` lint --- clippy_lints/src/declared_lints.rs | 1 - clippy_lints/src/deprecated_lints.rs | 2 ++ clippy_lints/src/methods/mod.rs | 28 ---------------- clippy_lints/src/methods/option_map_or_err_ok.rs | 41 ------------------------ tests/ui/deprecated.rs | 1 + tests/ui/deprecated.stderr | 8 ++++- tests/ui/manual_ok_or.stderr | 11 +------ tests/ui/option_map_or_err_ok.fixed | 7 ---- tests/ui/option_map_or_err_ok.rs | 7 ---- tests/ui/option_map_or_err_ok.stderr | 11 ------- 10 files changed, 11 insertions(+), 106 deletions(-) delete mode 100644 clippy_lints/src/methods/option_map_or_err_ok.rs delete mode 100644 tests/ui/option_map_or_err_ok.fixed delete mode 100644 tests/ui/option_map_or_err_ok.rs delete mode 100644 tests/ui/option_map_or_err_ok.stderr diff --git a/clippy_lints/src/declared_lints.rs b/clippy_lints/src/declared_lints.rs index 6d6b415f8cd..01d0c919b21 100644 --- a/clippy_lints/src/declared_lints.rs +++ b/clippy_lints/src/declared_lints.rs @@ -449,7 +449,6 @@ pub static LINTS: &[&crate::LintInfo] = &[ crate::methods::OPTION_AS_REF_CLONED_INFO, crate::methods::OPTION_AS_REF_DEREF_INFO, crate::methods::OPTION_FILTER_MAP_INFO, - crate::methods::OPTION_MAP_OR_ERR_OK_INFO, crate::methods::OPTION_MAP_OR_NONE_INFO, crate::methods::OR_FUN_CALL_INFO, crate::methods::OR_THEN_UNWRAP_INFO, diff --git a/clippy_lints/src/deprecated_lints.rs b/clippy_lints/src/deprecated_lints.rs index 3ea792d8b83..9d0ad3a6398 100644 --- a/clippy_lints/src/deprecated_lints.rs +++ b/clippy_lints/src/deprecated_lints.rs @@ -40,6 +40,8 @@ declare_with_version! { DEPRECATED(DEPRECATED_VERSION): &[(&str, &str)] = &[ ("clippy::pub_enum_variant_names", "`clippy::enum_variant_names` now covers this case via the `avoid-breaking-exported-api` config"), #[clippy::version = "1.54.0"] ("clippy::wrong_pub_self_convention", "`clippy::wrong_self_convention` now covers this case via the `avoid-breaking-exported-api` config"), + #[clippy::version = "1.86.0"] + ("clippy::option_map_or_err_ok", "`clippy::manual_ok_or` covers this case"), // end deprecated lints. used by `cargo dev deprecate_lint` ]} diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 9bfa5947990..5dad6c2e5df 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -82,7 +82,6 @@ mod ok_expect; mod open_options; mod option_as_ref_cloned; mod option_as_ref_deref; -mod option_map_or_err_ok; mod option_map_or_none; mod option_map_unwrap_or; mod or_fun_call; @@ -3782,31 +3781,6 @@ declare_clippy_lint! { "calls to `Path::join` which will overwrite the original path" } -declare_clippy_lint! { - /// ### What it does - /// Checks for usage of `_.map_or(Err(_), Ok)`. - /// - /// ### Why is this bad? - /// Readability, this can be written more concisely as - /// `_.ok_or(_)`. - /// - /// ### Example - /// ```no_run - /// # let opt = Some(1); - /// opt.map_or(Err("error"), Ok); - /// ``` - /// - /// Use instead: - /// ```no_run - /// # let opt = Some(1); - /// opt.ok_or("error"); - /// ``` - #[clippy::version = "1.76.0"] - pub OPTION_MAP_OR_ERR_OK, - style, - "using `Option.map_or(Err(_), Ok)`, which is more succinctly expressed as `Option.ok_or(_)`" -} - declare_clippy_lint! { /// ### What it does /// Checks for iterators of `Result`s using `.filter(Result::is_ok).map(Result::unwrap)` that may @@ -4510,7 +4484,6 @@ impl_lint_pass!(Methods => [ WAKER_CLONE_WAKE, UNNECESSARY_FALLIBLE_CONVERSIONS, JOIN_ABSOLUTE_PATHS, - OPTION_MAP_OR_ERR_OK, RESULT_FILTER_MAP, ITER_FILTER_IS_SOME, ITER_FILTER_IS_OK, @@ -5069,7 +5042,6 @@ impl Methods { ("map_or", [def, map]) => { option_map_or_none::check(cx, expr, recv, def, map); manual_ok_or::check(cx, expr, recv, def, map); - option_map_or_err_ok::check(cx, expr, recv, def, map); unnecessary_map_or::check(cx, expr, recv, def, map, span, &self.msrv); }, ("map_or_else", [def, map]) => { diff --git a/clippy_lints/src/methods/option_map_or_err_ok.rs b/clippy_lints/src/methods/option_map_or_err_ok.rs deleted file mode 100644 index 4e424d4c066..00000000000 --- a/clippy_lints/src/methods/option_map_or_err_ok.rs +++ /dev/null @@ -1,41 +0,0 @@ -use clippy_utils::diagnostics::span_lint_and_sugg; -use clippy_utils::source::snippet; -use clippy_utils::ty::is_type_diagnostic_item; -use clippy_utils::{is_res_lang_ctor, path_res}; -use rustc_errors::Applicability; -use rustc_hir::LangItem::{ResultErr, ResultOk}; -use rustc_hir::{Expr, ExprKind}; -use rustc_lint::LateContext; -use rustc_span::symbol::sym; - -use super::OPTION_MAP_OR_ERR_OK; - -pub(super) fn check<'tcx>( - cx: &LateContext<'tcx>, - expr: &'tcx Expr<'tcx>, - recv: &'tcx Expr<'_>, - or_expr: &'tcx Expr<'_>, - map_expr: &'tcx Expr<'_>, -) { - // We check that it's called on an `Option` type. - if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Option) - // We check that first we pass an `Err`. - && let ExprKind::Call(call, &[arg]) = or_expr.kind - && is_res_lang_ctor(cx, path_res(cx, call), ResultErr) - // And finally we check that it is mapped as `Ok`. - && is_res_lang_ctor(cx, path_res(cx, map_expr), ResultOk) - { - let msg = "called `map_or(Err(_), Ok)` on an `Option` value"; - let self_snippet = snippet(cx, recv.span, ".."); - let err_snippet = snippet(cx, arg.span, ".."); - span_lint_and_sugg( - cx, - OPTION_MAP_OR_ERR_OK, - expr.span, - msg, - "consider using `ok_or`", - format!("{self_snippet}.ok_or({err_snippet})"), - Applicability::MachineApplicable, - ); - } -} diff --git a/tests/ui/deprecated.rs b/tests/ui/deprecated.rs index 5617db90a47..35646e1c239 100644 --- a/tests/ui/deprecated.rs +++ b/tests/ui/deprecated.rs @@ -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() {} diff --git a/tests/ui/deprecated.stderr b/tests/ui/deprecated.stderr index b3e1646c804..d7be1e583b0 100644 --- a/tests/ui/deprecated.stderr +++ b/tests/ui/deprecated.stderr @@ -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 diff --git a/tests/ui/manual_ok_or.stderr b/tests/ui/manual_ok_or.stderr index 2441a75b5c4..4722f53580f 100644 --- a/tests/ui/manual_ok_or.stderr +++ b/tests/ui/manual_ok_or.stderr @@ -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 diff --git a/tests/ui/option_map_or_err_ok.fixed b/tests/ui/option_map_or_err_ok.fixed deleted file mode 100644 index 131f4b2093e..00000000000 --- a/tests/ui/option_map_or_err_ok.fixed +++ /dev/null @@ -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 -} diff --git a/tests/ui/option_map_or_err_ok.rs b/tests/ui/option_map_or_err_ok.rs deleted file mode 100644 index 0f07a592ae5..00000000000 --- a/tests/ui/option_map_or_err_ok.rs +++ /dev/null @@ -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 -} diff --git a/tests/ui/option_map_or_err_ok.stderr b/tests/ui/option_map_or_err_ok.stderr deleted file mode 100644 index 1971af80aa8..00000000000 --- a/tests/ui/option_map_or_err_ok.stderr +++ /dev/null @@ -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 - -- cgit 1.4.1-3-g733a5 From a03242f8e0e7ac1d15b2e78568dcbb5e92e9f258 Mon Sep 17 00:00:00 2001 From: Samuel Tardieu Date: Sat, 18 Jan 2025 17:17:41 +0100 Subject: Move `manual_ok_or` from pedantic to style MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `manual_ok_or` covers the same case that were covered by `option_map_or_err_ok` which is not deprecated. The latter was in the "style" category. Also, the lint is machine applicable, and leads to shorter and more readable code, so "style" is appropriate. The only difference is that the η-expanded form of `Result::Ok()` was not covered by `option_map_or_err_ok` while it is by `manual_ok_or`, so the category change may expose some new occurrences. --- clippy_lints/src/methods/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 5dad6c2e5df..7625057368e 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -2639,7 +2639,7 @@ declare_clippy_lint! { /// ``` #[clippy::version = "1.49.0"] pub MANUAL_OK_OR, - pedantic, + style, "finds patterns that can be encoded more concisely with `Option::ok_or`" } -- cgit 1.4.1-3-g733a5