Skip to content

Commit 411992a

Browse files
committed
fix: map_unwrap_or FN on references
1 parent fcaa02f commit 411992a

File tree

5 files changed

+88
-13
lines changed

5 files changed

+88
-13
lines changed

clippy_lints/src/methods/map_unwrap_or.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ pub(super) fn check<'tcx>(
2626
map_span: Span,
2727
msrv: Msrv,
2828
) {
29-
let is_option = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Option);
30-
let is_result = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Result);
29+
let recv_ty = cx.typeck_results().expr_ty(recv).peel_refs();
30+
let is_option = is_type_diagnostic_item(cx, recv_ty, sym::Option);
31+
let is_result = is_type_diagnostic_item(cx, recv_ty, sym::Result);
3132

3233
if is_result && !msrv.meets(cx, msrvs::RESULT_MAP_OR) {
3334
return;

clippy_lints/src/methods/map_unwrap_or_else.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ pub(super) fn check<'tcx>(
2121
unwrap_arg: &'tcx hir::Expr<'_>,
2222
msrv: Msrv,
2323
) -> bool {
24-
// lint if the caller of `map()` is an `Option` or a `Result`.
25-
let is_option = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Option);
26-
let is_result = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Result);
24+
let recv_ty = cx.typeck_results().expr_ty(recv).peel_refs();
25+
let is_option = is_type_diagnostic_item(cx, recv_ty, sym::Option);
26+
let is_result = is_type_diagnostic_item(cx, recv_ty, sym::Result);
2727

2828
if is_result && !msrv.meets(cx, msrvs::RESULT_MAP_OR_ELSE) {
2929
return false;

tests/ui/map_unwrap_or_fixable.fixed

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@aux-build:option_helpers.rs
22

33
#![warn(clippy::map_unwrap_or)]
4+
#![allow(clippy::unnecessary_lazy_evaluations)]
45

56
#[macro_use]
67
extern crate option_helpers;
@@ -67,3 +68,21 @@ fn issue15714() {
6768
println!("{}", r.is_ok_and(|y| y == 1));
6869
//~^ map_unwrap_or
6970
}
71+
72+
fn issue15713() {
73+
let x = &Some(3);
74+
println!("{}", x.map_or(3, |y| y + 1));
75+
//~^ map_unwrap_or
76+
77+
let x: &Result<i32, ()> = &Ok(3);
78+
println!("{}", x.map_or(3, |y| y + 1));
79+
//~^ map_unwrap_or
80+
81+
let x = &Some(3);
82+
println!("{}", x.map_or_else(|| 3, |y| y + 1));
83+
//~^ map_unwrap_or
84+
85+
let x: &Result<i32, ()> = &Ok(3);
86+
println!("{}", x.map_or_else(|_| 3, |y| y + 1));
87+
//~^ map_unwrap_or
88+
}

tests/ui/map_unwrap_or_fixable.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@aux-build:option_helpers.rs
22

33
#![warn(clippy::map_unwrap_or)]
4+
#![allow(clippy::unnecessary_lazy_evaluations)]
45

56
#[macro_use]
67
extern crate option_helpers;
@@ -73,3 +74,21 @@ fn issue15714() {
7374
println!("{}", r.map(|y| y == 1).unwrap_or(false));
7475
//~^ map_unwrap_or
7576
}
77+
78+
fn issue15713() {
79+
let x = &Some(3);
80+
println!("{}", x.map(|y| y + 1).unwrap_or(3));
81+
//~^ map_unwrap_or
82+
83+
let x: &Result<i32, ()> = &Ok(3);
84+
println!("{}", x.map(|y| y + 1).unwrap_or(3));
85+
//~^ map_unwrap_or
86+
87+
let x = &Some(3);
88+
println!("{}", x.map(|y| y + 1).unwrap_or_else(|| 3));
89+
//~^ map_unwrap_or
90+
91+
let x: &Result<i32, ()> = &Ok(3);
92+
println!("{}", x.map(|y| y + 1).unwrap_or_else(|_| 3));
93+
//~^ map_unwrap_or
94+
}

tests/ui/map_unwrap_or_fixable.stderr

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: called `map(<f>).unwrap_or_else(<g>)` on an `Option` value
2-
--> tests/ui/map_unwrap_or_fixable.rs:16:13
2+
--> tests/ui/map_unwrap_or_fixable.rs:17:13
33
|
44
LL | let _ = opt.map(|x| x + 1)
55
| _____________^
@@ -11,7 +11,7 @@ LL | | .unwrap_or_else(|| 0);
1111
= help: to override `-D warnings` add `#[allow(clippy::map_unwrap_or)]`
1212

1313
error: called `map(<f>).unwrap_or_else(<g>)` on a `Result` value
14-
--> tests/ui/map_unwrap_or_fixable.rs:47:13
14+
--> tests/ui/map_unwrap_or_fixable.rs:48:13
1515
|
1616
LL | let _ = res.map(|x| x + 1)
1717
| _____________^
@@ -20,7 +20,7 @@ LL | | .unwrap_or_else(|_e| 0);
2020
| |_______________________________^ help: try: `res.map_or_else(|_e| 0, |x| x + 1)`
2121

2222
error: called `map(<f>).unwrap_or(<a>)` on an `Option` value
23-
--> tests/ui/map_unwrap_or_fixable.rs:64:20
23+
--> tests/ui/map_unwrap_or_fixable.rs:65:20
2424
|
2525
LL | println!("{}", o.map(|y| y + 1).unwrap_or(3));
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -32,13 +32,13 @@ LL + println!("{}", o.map_or(3, |y| y + 1));
3232
|
3333

3434
error: called `map(<f>).unwrap_or_else(<g>)` on an `Option` value
35-
--> tests/ui/map_unwrap_or_fixable.rs:66:20
35+
--> tests/ui/map_unwrap_or_fixable.rs:67:20
3636
|
3737
LL | println!("{}", o.map(|y| y + 1).unwrap_or_else(|| 3));
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `o.map_or_else(|| 3, |y| y + 1)`
3939

4040
error: called `map(<f>).unwrap_or(<a>)` on an `Result` value
41-
--> tests/ui/map_unwrap_or_fixable.rs:68:20
41+
--> tests/ui/map_unwrap_or_fixable.rs:69:20
4242
|
4343
LL | println!("{}", r.map(|y| y + 1).unwrap_or(3));
4444
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -50,13 +50,13 @@ LL + println!("{}", r.map_or(3, |y| y + 1));
5050
|
5151

5252
error: called `map(<f>).unwrap_or_else(<g>)` on a `Result` value
53-
--> tests/ui/map_unwrap_or_fixable.rs:70:20
53+
--> tests/ui/map_unwrap_or_fixable.rs:71:20
5454
|
5555
LL | println!("{}", r.map(|y| y + 1).unwrap_or_else(|()| 3));
5656
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `r.map_or_else(|()| 3, |y| y + 1)`
5757

5858
error: called `map(<f>).unwrap_or(false)` on an `Result` value
59-
--> tests/ui/map_unwrap_or_fixable.rs:73:20
59+
--> tests/ui/map_unwrap_or_fixable.rs:74:20
6060
|
6161
LL | println!("{}", r.map(|y| y == 1).unwrap_or(false));
6262
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -67,5 +67,41 @@ LL - println!("{}", r.map(|y| y == 1).unwrap_or(false));
6767
LL + println!("{}", r.is_ok_and(|y| y == 1));
6868
|
6969

70-
error: aborting due to 7 previous errors
70+
error: called `map(<f>).unwrap_or(<a>)` on an `Option` value
71+
--> tests/ui/map_unwrap_or_fixable.rs:80:20
72+
|
73+
LL | println!("{}", x.map(|y| y + 1).unwrap_or(3));
74+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
75+
|
76+
help: use `map_or(<a>, <f>)` instead
77+
|
78+
LL - println!("{}", x.map(|y| y + 1).unwrap_or(3));
79+
LL + println!("{}", x.map_or(3, |y| y + 1));
80+
|
81+
82+
error: called `map(<f>).unwrap_or(<a>)` on an `Result` value
83+
--> tests/ui/map_unwrap_or_fixable.rs:84:20
84+
|
85+
LL | println!("{}", x.map(|y| y + 1).unwrap_or(3));
86+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
87+
|
88+
help: use `map_or(<a>, <f>)` instead
89+
|
90+
LL - println!("{}", x.map(|y| y + 1).unwrap_or(3));
91+
LL + println!("{}", x.map_or(3, |y| y + 1));
92+
|
93+
94+
error: called `map(<f>).unwrap_or_else(<g>)` on an `Option` value
95+
--> tests/ui/map_unwrap_or_fixable.rs:88:20
96+
|
97+
LL | println!("{}", x.map(|y| y + 1).unwrap_or_else(|| 3));
98+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `x.map_or_else(|| 3, |y| y + 1)`
99+
100+
error: called `map(<f>).unwrap_or_else(<g>)` on a `Result` value
101+
--> tests/ui/map_unwrap_or_fixable.rs:92:20
102+
|
103+
LL | println!("{}", x.map(|y| y + 1).unwrap_or_else(|_| 3));
104+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `x.map_or_else(|_| 3, |y| y + 1)`
105+
106+
error: aborting due to 11 previous errors
71107

0 commit comments

Comments
 (0)