Add test for manual_unwrap_or in issue 13018

This commit is contained in:
Lzu Tao 2024-07-07 08:11:32 +07:00
parent 0c9016aa1e
commit e864519fbc
3 changed files with 33 additions and 1 deletions

View file

@ -234,4 +234,13 @@ fn implicit_deref_ref() {
}; };
} }
mod issue_13018 {
use std::collections::HashMap;
type RefName = i32;
pub fn get(index: &HashMap<usize, Vec<RefName>>, id: usize) -> &[RefName] {
index.get(&id).unwrap_or(&[])
}
}
fn main() {} fn main() {}

View file

@ -284,4 +284,17 @@ fn implicit_deref_ref() {
}; };
} }
mod issue_13018 {
use std::collections::HashMap;
type RefName = i32;
pub fn get(index: &HashMap<usize, Vec<RefName>>, id: usize) -> &[RefName] {
if let Some(names) = index.get(&id) {
names
} else {
&[]
}
}
}
fn main() {} fn main() {}

View file

@ -172,5 +172,15 @@ LL | | None => 0,
LL | | }; LL | | };
| |_________^ help: replace with: `some_macro!().unwrap_or(0)` | |_________^ help: replace with: `some_macro!().unwrap_or(0)`
error: aborting due to 16 previous errors error: this pattern reimplements `Option::unwrap_or`
--> tests/ui/manual_unwrap_or.rs:292:9
|
LL | / if let Some(names) = index.get(&id) {
LL | | names
LL | | } else {
LL | | &[]
LL | | }
| |_________^ help: replace with: `index.get(&id).unwrap_or(&[])`
error: aborting due to 17 previous errors