Skip to content

Commit

Permalink
Add new ui test for match_option_and_default
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Mar 8, 2024
1 parent c6b419b commit 8e78814
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
19 changes: 19 additions & 0 deletions tests/ui/match_option_and_default.fixed
@@ -0,0 +1,19 @@
#![warn(clippy::match_option_and_default)]
#![allow(clippy::unnecessary_literal_unwrap)]

fn main() {
let x: Option<Vec<String>> = None;
x.unwrap_or_default();

let x: Option<Vec<String>> = None;
x.unwrap_or_default();

let x: Option<String> = None;
x.unwrap_or_default();

let x: Option<Vec<String>> = None;
x.unwrap_or_default();

let x: Option<Vec<String>> = None;
x.unwrap_or_default();
}
40 changes: 40 additions & 0 deletions tests/ui/match_option_and_default.rs
@@ -0,0 +1,40 @@
#![warn(clippy::match_option_and_default)]
#![allow(clippy::unnecessary_literal_unwrap)]

fn main() {
let x: Option<Vec<String>> = None;
match x {
//~^ ERROR: match can be simplified with `.unwrap_or_default()`
Some(v) => v,
None => Vec::default(),
};

let x: Option<Vec<String>> = None;
match x {
//~^ ERROR: match can be simplified with `.unwrap_or_default()`
Some(v) => v,
_ => Vec::default(),
};

let x: Option<String> = None;
match x {
//~^ ERROR: match can be simplified with `.unwrap_or_default()`
Some(v) => v,
None => String::new(),
};

let x: Option<Vec<String>> = None;
match x {
//~^ ERROR: match can be simplified with `.unwrap_or_default()`
None => Vec::default(),
Some(v) => v,
};

let x: Option<Vec<String>> = None;
if let Some(v) = x {
//~^ ERROR: if let can be simplified with `.unwrap_or_default()`
v
} else {
Vec::default()
};
}
56 changes: 56 additions & 0 deletions tests/ui/match_option_and_default.stderr
@@ -0,0 +1,56 @@
error: match can be simplified with `.unwrap_or_default()`
--> tests/ui/match_option_and_default.rs:6:5
|
LL | / match x {
LL | |
LL | | Some(v) => v,
LL | | None => Vec::default(),
LL | | };
| |_____^ help: replace it with: `x.unwrap_or_default()`
|
= note: `-D clippy::match-option-and-default` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::match_option_and_default)]`

error: match can be simplified with `.unwrap_or_default()`
--> tests/ui/match_option_and_default.rs:13:5
|
LL | / match x {
LL | |
LL | | Some(v) => v,
LL | | _ => Vec::default(),
LL | | };
| |_____^ help: replace it with: `x.unwrap_or_default()`

error: match can be simplified with `.unwrap_or_default()`
--> tests/ui/match_option_and_default.rs:20:5
|
LL | / match x {
LL | |
LL | | Some(v) => v,
LL | | None => String::new(),
LL | | };
| |_____^ help: replace it with: `x.unwrap_or_default()`

error: match can be simplified with `.unwrap_or_default()`
--> tests/ui/match_option_and_default.rs:27:5
|
LL | / match x {
LL | |
LL | | None => Vec::default(),
LL | | Some(v) => v,
LL | | };
| |_____^ help: replace it with: `x.unwrap_or_default()`

error: if let can be simplified with `.unwrap_or_default()`
--> tests/ui/match_option_and_default.rs:34:5
|
LL | / if let Some(v) = x {
LL | |
LL | | v
LL | | } else {
LL | | Vec::default()
LL | | };
| |_____^ help: replace it with: `x.unwrap_or_default()`

error: aborting due to 5 previous errors

0 comments on commit 8e78814

Please sign in to comment.