Skip to content

Commit 8097bb2

Browse files
committed
check for presence of partialeq on linted types
1 parent 895aee2 commit 8097bb2

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

clippy_lints/src/methods/unnecessary_map_or.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
55
use clippy_utils::eager_or_lazy::switch_to_eager_eval;
66
use clippy_utils::source::snippet_opt;
77
use clippy_utils::sugg::{Sugg, make_binop};
8-
use clippy_utils::ty::get_type_diagnostic_name;
8+
use clippy_utils::ty::{get_type_diagnostic_name, implements_trait};
99
use clippy_utils::visitors::is_local_used;
1010
use clippy_utils::{is_from_proc_macro, path_to_local_id};
1111
use rustc_ast::LitKind::Bool;
@@ -80,6 +80,8 @@ pub(super) fn check<'a>(
8080
&& !is_local_used(cx, non_binding_location, hir_id)
8181
&& let typeck_results = cx.typeck_results()
8282
&& typeck_results.expr_ty(l) == typeck_results.expr_ty(r)
83+
&& let Some(partial_eq) = cx.tcx.get_diagnostic_item(sym::PartialEq)
84+
&& implements_trait(cx, recv_ty, partial_eq, &[recv_ty.into()])
8385
{
8486
let wrap = variant.variant_name();
8587

tests/ui/unnecessary_map_or.fixed

+5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ fn main() {
4545
with_span! {
4646
let _ = Some(5).map_or(false, |n| n == 5);
4747
}
48+
49+
// check for presence of PartialEq, and alter suggestion to use `is_ok_and` if absent
50+
struct S;
51+
let r: Result<i32, S> = Ok(3);
52+
let _ = r.is_ok_and(|x| x == 7);
4853
}
4954

5055
#[clippy::msrv = "1.69.0"]

tests/ui/unnecessary_map_or.rs

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ fn main() {
4848
with_span! {
4949
let _ = Some(5).map_or(false, |n| n == 5);
5050
}
51+
52+
// check for presence of PartialEq, and alter suggestion to use `is_ok_and` if absent
53+
struct S;
54+
let r: Result<i32, S> = Ok(3);
55+
let _ = r.map_or(false, |x| x == 7);
5156
}
5257

5358
#[clippy::msrv = "1.69.0"]

tests/ui/unnecessary_map_or.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,11 @@ error: this `map_or` is redundant
8383
LL | let _ = Some(5).map_or(false, |n| n == 5).then(|| 1);
8484
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use a standard comparison instead: `(Some(5) == Some(5))`
8585

86-
error: aborting due to 11 previous errors
86+
error: this `map_or` is redundant
87+
--> tests/ui/unnecessary_map_or.rs:55:13
88+
|
89+
LL | let _ = r.map_or(false, |x| x == 7);
90+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use is_ok_and instead: `r.is_ok_and(|x| x == 7)`
91+
92+
error: aborting due to 12 previous errors
8793

0 commit comments

Comments
 (0)