Skip to content

Commit fadb254

Browse files
Add new ui test for match_option_and_default
1 parent 1abf441 commit fadb254

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![warn(clippy::match_option_and_default)]
2+
#![allow(clippy::unnecessary_literal_unwrap)]
3+
4+
fn main() {
5+
let x: Option<Vec<String>> = None;
6+
x.unwrap_or_default();
7+
8+
let x: Option<Vec<String>> = None;
9+
x.unwrap_or_default();
10+
11+
let x: Option<String> = None;
12+
x.unwrap_or_default();
13+
14+
let x: Option<Vec<String>> = None;
15+
x.unwrap_or_default();
16+
17+
let x: Option<Vec<String>> = None;
18+
x.unwrap_or_default();
19+
}

tests/ui/match_option_and_default.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#![warn(clippy::match_option_and_default)]
2+
#![allow(clippy::unnecessary_literal_unwrap)]
3+
4+
fn main() {
5+
let x: Option<Vec<String>> = None;
6+
match x {
7+
//~^ ERROR: match can be simplified with `.unwrap_or_default()`
8+
Some(v) => v,
9+
None => Vec::default(),
10+
};
11+
12+
let x: Option<Vec<String>> = None;
13+
match x {
14+
//~^ ERROR: match can be simplified with `.unwrap_or_default()`
15+
Some(v) => v,
16+
_ => Vec::default(),
17+
};
18+
19+
let x: Option<String> = None;
20+
match x {
21+
//~^ ERROR: match can be simplified with `.unwrap_or_default()`
22+
Some(v) => v,
23+
None => String::new(),
24+
};
25+
26+
let x: Option<Vec<String>> = None;
27+
match x {
28+
//~^ ERROR: match can be simplified with `.unwrap_or_default()`
29+
None => Vec::default(),
30+
Some(v) => v,
31+
};
32+
33+
let x: Option<Vec<String>> = None;
34+
if let Some(v) = x {
35+
//~^ ERROR: if let can be simplified with `.unwrap_or_default()`
36+
v
37+
} else {
38+
Vec::default()
39+
};
40+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
error: match can be simplified with `.unwrap_or_default()`
2+
--> tests/ui/match_option_and_default.rs:6:5
3+
|
4+
LL | / match x {
5+
LL | |
6+
LL | | Some(v) => v,
7+
LL | | None => Vec::default(),
8+
LL | | };
9+
| |_____^ help: replace it with: `x.unwrap_or_default()`
10+
|
11+
= note: `-D clippy::match-option-and-default` implied by `-D warnings`
12+
= help: to override `-D warnings` add `#[allow(clippy::match_option_and_default)]`
13+
14+
error: match can be simplified with `.unwrap_or_default()`
15+
--> tests/ui/match_option_and_default.rs:13:5
16+
|
17+
LL | / match x {
18+
LL | |
19+
LL | | Some(v) => v,
20+
LL | | _ => Vec::default(),
21+
LL | | };
22+
| |_____^ help: replace it with: `x.unwrap_or_default()`
23+
24+
error: match can be simplified with `.unwrap_or_default()`
25+
--> tests/ui/match_option_and_default.rs:20:5
26+
|
27+
LL | / match x {
28+
LL | |
29+
LL | | Some(v) => v,
30+
LL | | None => String::new(),
31+
LL | | };
32+
| |_____^ help: replace it with: `x.unwrap_or_default()`
33+
34+
error: match can be simplified with `.unwrap_or_default()`
35+
--> tests/ui/match_option_and_default.rs:27:5
36+
|
37+
LL | / match x {
38+
LL | |
39+
LL | | None => Vec::default(),
40+
LL | | Some(v) => v,
41+
LL | | };
42+
| |_____^ help: replace it with: `x.unwrap_or_default()`
43+
44+
error: if let can be simplified with `.unwrap_or_default()`
45+
--> tests/ui/match_option_and_default.rs:34:5
46+
|
47+
LL | / if let Some(v) = x {
48+
LL | |
49+
LL | | v
50+
LL | | } else {
51+
LL | | Vec::default()
52+
LL | | };
53+
| |_____^ help: replace it with: `x.unwrap_or_default()`
54+
55+
error: aborting due to 5 previous errors
56+

0 commit comments

Comments
 (0)