rust-clippy/tests/ui/or_then_unwrap.rs

51 lines
1.5 KiB
Rust
Raw Normal View History

#![warn(clippy::or_then_unwrap)]
2023-05-31 17:47:10 +00:00
#![allow(clippy::map_identity, clippy::let_unit_value, clippy::unnecessary_literal_unwrap)]
2022-03-17 17:57:28 +00:00
2022-03-27 12:41:09 +00:00
struct SomeStruct;
2022-03-17 17:57:28 +00:00
impl SomeStruct {
2022-03-17 18:13:44 +00:00
fn or(self, _: Option<Self>) -> Self {
self
}
fn unwrap(&self) {}
2022-03-17 17:57:28 +00:00
}
2022-03-27 12:41:09 +00:00
struct SomeOtherStruct;
2022-03-17 23:51:26 +00:00
impl SomeOtherStruct {
fn or(self) -> Self {
self
}
fn unwrap(&self) {}
}
2022-03-17 17:57:28 +00: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
2022-03-19 17:17:43 +00:00
// as part of a method chain
let option: Option<&str> = None;
let _ = option.map(|v| v).or(Some("fallback")).unwrap().to_string().chars(); // should trigger lint
2022-03-17 17:57:28 +00:00
// Not Option/Result
let instance = SomeStruct {};
let _ = instance.or(Some(SomeStruct {})).unwrap(); // should not trigger lint
2022-03-18 13:45:48 +00:00
// or takes no argument
2022-03-17 23:51:26 +00:00
let instance = SomeOtherStruct {};
let _ = instance.or().unwrap(); // should not trigger lint and should not panic
2022-03-17 17:57:28 +00: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 23:51:26 +00: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 17:57:28 +00:00
}