rust/tests/ui/use_unwrap_or.rs

46 lines
1.2 KiB
Rust
Raw Normal View History

2022-03-17 12:57:28 -05:00
#![warn(clippy::use_unwrap_or)]
2022-03-17 18:51:26 -05:00
#![allow(clippy::map_identity)]
2022-03-17 12:57:28 -05:00
struct SomeStruct {}
impl SomeStruct {
2022-03-17 13:13:44 -05:00
fn or(self, _: Option<Self>) -> Self {
self
}
fn unwrap(&self) {}
2022-03-17 12:57:28 -05:00
}
2022-03-17 18:51:26 -05:00
struct SomeOtherStruct {}
impl SomeOtherStruct {
fn or(self) -> Self {
self
}
fn unwrap(&self) {}
}
2022-03-17 12:57:28 -05:00
fn main() {
let option: Option<&str> = None;
let _ = option.or(Some("fallback")).unwrap(); // should trigger lint
let result: Result<&str, &str> = Err("Error");
let _ = result.or::<&str>(Ok("fallback")).unwrap(); // should trigger lint
// Not Option/Result
let instance = SomeStruct {};
let _ = instance.or(Some(SomeStruct {})).unwrap(); // should not trigger lint
2022-03-17 18:51:26 -05:00
let instance = SomeOtherStruct {};
let _ = instance.or().unwrap(); // should not trigger lint and should not panic
2022-03-17 12:57:28 -05:00
// None in or
let option: Option<&str> = None;
let _ = option.or(None).unwrap(); // should not trigger lint
// Not Err in or
let result: Result<&str, &str> = Err("Error");
let _ = result.or::<&str>(Err("Other Error")).unwrap(); // should not trigger lint
2022-03-17 18:51:26 -05:00
// other function between
let option: Option<&str> = None;
let _ = option.or(Some("fallback")).map(|v| v).unwrap(); // should not trigger lint
2022-03-17 12:57:28 -05:00
}