Add new ui test for match_option_and_default

This commit is contained in:
Guillaume Gomez 2024-03-08 22:04:31 +01:00
parent 1abf4418f8
commit fadb254073
3 changed files with 115 additions and 0 deletions

View file

@ -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();
}

View file

@ -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()
};
}

View file

@ -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