Skip to content

Commit

Permalink
Make unnecessary_map_or work with ref and Deref
Browse files Browse the repository at this point in the history
Receivers which are references to `Option` and `Result`, or who
implement `Deref` to one of those types, will be linted as well.
  • Loading branch information
samueltardieu committed Jan 19, 2025
1 parent 7f37b2a commit 27592e3
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/methods/unnecessary_map_or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub(super) fn check<'a>(
return;
};

let recv_ty = cx.typeck_results().expr_ty(recv);
let recv_ty = cx.typeck_results().expr_ty_adjusted(recv);

let Bool(def_bool) = def_kind.node else {
return;
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/unnecessary_map_or.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,20 @@ fn msrv_1_81() {
// is_none_or added in 1.82.0
let _ = Some(5).map_or(true, |n| n == if 2 > 1 { n } else { 0 });
}

fn with_refs(o: &mut Option<u32>) -> bool {
o.is_none_or(|n| n > 5) || (o as &Option<u32>).is_none_or(|n| n < 5)
}

struct S;

impl std::ops::Deref for S {
type Target = Option<u32>;
fn deref(&self) -> &Self::Target {
&Some(0)
}
}

fn with_deref(o: &S) -> bool {
o.is_none_or(|n| n > 5)
}
17 changes: 17 additions & 0 deletions tests/ui/unnecessary_map_or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,20 @@ fn msrv_1_81() {
// is_none_or added in 1.82.0
let _ = Some(5).map_or(true, |n| n == if 2 > 1 { n } else { 0 });
}

fn with_refs(o: &mut Option<u32>) -> bool {
o.map_or(true, |n| n > 5) || (o as &Option<u32>).map_or(true, |n| n < 5)
}

struct S;

impl std::ops::Deref for S {
type Target = Option<u32>;
fn deref(&self) -> &Self::Target {
&Some(0)
}
}

fn with_deref(o: &S) -> bool {
o.map_or(true, |n| n > 5)
}
38 changes: 37 additions & 1 deletion tests/ui/unnecessary_map_or.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -251,5 +251,41 @@ help: use a standard comparison instead
LL | let _ = r == Ok(8);
| ~~~~~~~~~~

error: aborting due to 21 previous errors
error: this `map_or` can be simplified
--> tests/ui/unnecessary_map_or.rs:90:5
|
LL | o.map_or(true, |n| n > 5) || (o as &Option<u32>).map_or(true, |n| n < 5)
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use is_none_or instead
|
LL - o.map_or(true, |n| n > 5) || (o as &Option<u32>).map_or(true, |n| n < 5)
LL + o.is_none_or(|n| n > 5) || (o as &Option<u32>).map_or(true, |n| n < 5)
|

error: this `map_or` can be simplified
--> tests/ui/unnecessary_map_or.rs:90:34
|
LL | o.map_or(true, |n| n > 5) || (o as &Option<u32>).map_or(true, |n| n < 5)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use is_none_or instead
|
LL - o.map_or(true, |n| n > 5) || (o as &Option<u32>).map_or(true, |n| n < 5)
LL + o.map_or(true, |n| n > 5) || (o as &Option<u32>).is_none_or(|n| n < 5)
|

error: this `map_or` can be simplified
--> tests/ui/unnecessary_map_or.rs:103:5
|
LL | o.map_or(true, |n| n > 5)
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: use is_none_or instead
|
LL - o.map_or(true, |n| n > 5)
LL + o.is_none_or(|n| n > 5)
|

error: aborting due to 24 previous errors

0 comments on commit 27592e3

Please sign in to comment.