Skip to content

Commit 7ed7283

Browse files
committed
Recognize unwrap_or_else method
1 parent 8f83502 commit 7ed7283

File tree

5 files changed

+50
-16
lines changed

5 files changed

+50
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5248,6 +5248,7 @@ Released 2018-09-13
52485248
[`unnecessary_fold`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_fold
52495249
[`unnecessary_join`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_join
52505250
[`unnecessary_lazy_evaluations`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_lazy_evaluations
5251+
[`unnecessary_literal_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_literal_unwrap
52515252
[`unnecessary_mut_passed`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_mut_passed
52525253
[`unnecessary_operation`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_operation
52535254
[`unnecessary_owned_empty_strings`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_owned_empty_strings

clippy_lints/src/methods/mod.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3893,13 +3893,16 @@ impl Methods {
38933893
("unwrap_or_default", []) => {
38943894
unnecessary_literal_unwrap::check(cx, expr, recv, name);
38953895
}
3896-
("unwrap_or_else", [u_arg]) => match method_call(recv) {
3897-
Some(("map", recv, [map_arg], _, _))
3898-
if map_unwrap_or::check(cx, expr, recv, map_arg, u_arg, &self.msrv) => {},
3899-
_ => {
3900-
unwrap_or_else_default::check(cx, expr, recv, u_arg);
3901-
unnecessary_lazy_eval::check(cx, expr, recv, u_arg, "unwrap_or");
3902-
},
3896+
("unwrap_or_else", [u_arg]) => {
3897+
match method_call(recv) {
3898+
Some(("map", recv, [map_arg], _, _))
3899+
if map_unwrap_or::check(cx, expr, recv, map_arg, u_arg, &self.msrv) => {},
3900+
_ => {
3901+
unwrap_or_else_default::check(cx, expr, recv, u_arg);
3902+
unnecessary_lazy_eval::check(cx, expr, recv, u_arg, "unwrap_or");
3903+
},
3904+
}
3905+
unnecessary_literal_unwrap::check(cx, expr, recv, name);
39033906
},
39043907
("zip", [arg]) => {
39053908
if let ExprKind::MethodCall(name, iter_recv, [], _) = recv.kind

tests/ui/unnecessary_literal_unwrap.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//run-rustfix
22
#![warn(clippy::unnecessary_literal_unwrap)]
3+
#![allow(clippy::unnecessary_lazy_evaluations)]
34

45
fn unwrap_option() {
56
let _val = 1;
@@ -15,11 +16,13 @@ fn unwrap_result() {
1516
fn unwrap_methods_option() {
1617
let _val = 1;
1718
let _val = 1;
19+
let _val = 1;
1820
}
1921

2022
fn unwrap_methods_result() {
2123
let _val = 1;
2224
let _val = 1;
25+
let _val = 1;
2326
}
2427

2528
fn main() {

tests/ui/unnecessary_literal_unwrap.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//run-rustfix
22
#![warn(clippy::unnecessary_literal_unwrap)]
3+
#![allow(clippy::unnecessary_lazy_evaluations)]
34

45
fn unwrap_option() {
56
let _val = Some(1).unwrap();
@@ -15,11 +16,13 @@ fn unwrap_result() {
1516
fn unwrap_methods_option() {
1617
let _val = Some(1).unwrap_or(2);
1718
let _val = Some(1).unwrap_or_default();
19+
let _val = Some(1).unwrap_or_else(|| _val);
1820
}
1921

2022
fn unwrap_methods_result() {
2123
let _val = Ok::<usize, ()>(1).unwrap_or(2);
2224
let _val = Ok::<usize, ()>(1).unwrap_or_default();
25+
let _val = Ok::<usize, ()>(1).unwrap_or_else(|()| _val);
2326
}
2427

2528
fn main() {

tests/ui/unnecessary_literal_unwrap.stderr

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: used `unwrap()` on `Some` value
2-
--> $DIR/unnecessary_literal_unwrap.rs:5:16
2+
--> $DIR/unnecessary_literal_unwrap.rs:6:16
33
|
44
LL | let _val = Some(1).unwrap();
55
| ^^^^^^^^^^^^^^^^
@@ -12,7 +12,7 @@ LL + let _val = 1;
1212
|
1313

1414
error: used `expect()` on `Some` value
15-
--> $DIR/unnecessary_literal_unwrap.rs:6:16
15+
--> $DIR/unnecessary_literal_unwrap.rs:7:16
1616
|
1717
LL | let _val = Some(1).expect("this never happens");
1818
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -24,7 +24,7 @@ LL + let _val = 1;
2424
|
2525

2626
error: used `unwrap()` on `Ok` value
27-
--> $DIR/unnecessary_literal_unwrap.rs:10:16
27+
--> $DIR/unnecessary_literal_unwrap.rs:11:16
2828
|
2929
LL | let _val = Ok::<usize, ()>(1).unwrap();
3030
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -36,7 +36,7 @@ LL + let _val = 1;
3636
|
3737

3838
error: used `expect()` on `Ok` value
39-
--> $DIR/unnecessary_literal_unwrap.rs:11:16
39+
--> $DIR/unnecessary_literal_unwrap.rs:12:16
4040
|
4141
LL | let _val = Ok::<usize, ()>(1).expect("this never happens");
4242
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -48,7 +48,7 @@ LL + let _val = 1;
4848
|
4949

5050
error: used `unwrap_or()` on `Some` value
51-
--> $DIR/unnecessary_literal_unwrap.rs:16:16
51+
--> $DIR/unnecessary_literal_unwrap.rs:17:16
5252
|
5353
LL | let _val = Some(1).unwrap_or(2);
5454
| ^^^^^^^^^^^^^^^^^^^^
@@ -60,7 +60,7 @@ LL + let _val = 1;
6060
|
6161

6262
error: used `unwrap_or_default()` on `Some` value
63-
--> $DIR/unnecessary_literal_unwrap.rs:17:16
63+
--> $DIR/unnecessary_literal_unwrap.rs:18:16
6464
|
6565
LL | let _val = Some(1).unwrap_or_default();
6666
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -71,8 +71,20 @@ LL - let _val = Some(1).unwrap_or_default();
7171
LL + let _val = 1;
7272
|
7373

74+
error: used `unwrap_or_else()` on `Some` value
75+
--> $DIR/unnecessary_literal_unwrap.rs:19:16
76+
|
77+
LL | let _val = Some(1).unwrap_or_else(|| _val);
78+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
79+
|
80+
help: remove the `Some` and `unwrap_or_else()`
81+
|
82+
LL - let _val = Some(1).unwrap_or_else(|| _val);
83+
LL + let _val = 1;
84+
|
85+
7486
error: used `unwrap_or()` on `Ok` value
75-
--> $DIR/unnecessary_literal_unwrap.rs:21:16
87+
--> $DIR/unnecessary_literal_unwrap.rs:23:16
7688
|
7789
LL | let _val = Ok::<usize, ()>(1).unwrap_or(2);
7890
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -84,7 +96,7 @@ LL + let _val = 1;
8496
|
8597

8698
error: used `unwrap_or_default()` on `Ok` value
87-
--> $DIR/unnecessary_literal_unwrap.rs:22:16
99+
--> $DIR/unnecessary_literal_unwrap.rs:24:16
88100
|
89101
LL | let _val = Ok::<usize, ()>(1).unwrap_or_default();
90102
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -95,5 +107,17 @@ LL - let _val = Ok::<usize, ()>(1).unwrap_or_default();
95107
LL + let _val = 1;
96108
|
97109

98-
error: aborting due to 8 previous errors
110+
error: used `unwrap_or_else()` on `Ok` value
111+
--> $DIR/unnecessary_literal_unwrap.rs:25:16
112+
|
113+
LL | let _val = Ok::<usize, ()>(1).unwrap_or_else(|()| _val);
114+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
115+
|
116+
help: remove the `Ok` and `unwrap_or_else()`
117+
|
118+
LL - let _val = Ok::<usize, ()>(1).unwrap_or_else(|()| _val);
119+
LL + let _val = 1;
120+
|
121+
122+
error: aborting due to 10 previous errors
99123

0 commit comments

Comments
 (0)