2020-10-06 04:49:08 -05:00
|
|
|
// run-rustfix
|
|
|
|
#![allow(dead_code)]
|
|
|
|
|
2020-10-17 18:11:59 -05:00
|
|
|
fn option_unwrap_or() {
|
2020-10-06 04:49:08 -05:00
|
|
|
// int case
|
|
|
|
Some(1).unwrap_or(42);
|
|
|
|
|
2020-10-11 15:55:05 -05:00
|
|
|
// int case reversed
|
|
|
|
Some(1).unwrap_or(42);
|
|
|
|
|
2020-10-06 04:49:08 -05:00
|
|
|
// richer none expr
|
2020-10-11 17:06:21 -05:00
|
|
|
Some(1).unwrap_or(1 + 42);
|
2020-10-06 04:49:08 -05:00
|
|
|
|
|
|
|
// multiline case
|
2020-10-14 15:09:28 -05:00
|
|
|
#[rustfmt::skip]
|
|
|
|
Some(1).unwrap_or({
|
|
|
|
42 + 42
|
|
|
|
+ 42 + 42 + 42
|
|
|
|
+ 42 + 42 + 42
|
2020-10-06 04:49:08 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
// string case
|
|
|
|
Some("Bob").unwrap_or("Alice");
|
|
|
|
|
|
|
|
// don't lint
|
|
|
|
match Some(1) {
|
|
|
|
Some(i) => i + 2,
|
|
|
|
None => 42,
|
|
|
|
};
|
|
|
|
match Some(1) {
|
|
|
|
Some(i) => i,
|
|
|
|
None => return,
|
|
|
|
};
|
|
|
|
for j in 0..4 {
|
|
|
|
match Some(j) {
|
|
|
|
Some(i) => i,
|
|
|
|
None => continue,
|
|
|
|
};
|
|
|
|
match Some(j) {
|
|
|
|
Some(i) => i,
|
|
|
|
None => break,
|
|
|
|
};
|
|
|
|
}
|
2020-10-14 15:09:28 -05:00
|
|
|
|
|
|
|
// cases where the none arm isn't a constant expression
|
|
|
|
// are not linted due to potential ownership issues
|
|
|
|
|
|
|
|
// ownership issue example, don't lint
|
|
|
|
struct NonCopyable;
|
|
|
|
let mut option: Option<NonCopyable> = None;
|
|
|
|
match option {
|
|
|
|
Some(x) => x,
|
|
|
|
None => {
|
|
|
|
option = Some(NonCopyable);
|
|
|
|
// some more code ...
|
|
|
|
option.unwrap()
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// ownership issue example, don't lint
|
|
|
|
let option: Option<&str> = None;
|
|
|
|
match option {
|
|
|
|
Some(s) => s,
|
|
|
|
None => &format!("{} {}!", "hello", "world"),
|
|
|
|
};
|
2020-10-06 04:49:08 -05:00
|
|
|
}
|
|
|
|
|
2020-10-17 18:11:59 -05:00
|
|
|
fn result_unwrap_or() {
|
|
|
|
// int case
|
|
|
|
(Ok(1) as Result<i32, &str>).unwrap_or(42);
|
|
|
|
|
|
|
|
// int case reversed
|
|
|
|
(Ok(1) as Result<i32, &str>).unwrap_or(42);
|
|
|
|
|
|
|
|
// richer none expr
|
|
|
|
(Ok(1) as Result<i32, &str>).unwrap_or(1 + 42);
|
|
|
|
|
|
|
|
// multiline case
|
|
|
|
#[rustfmt::skip]
|
|
|
|
(Ok(1) as Result<i32, &str>).unwrap_or({
|
|
|
|
42 + 42
|
|
|
|
+ 42 + 42 + 42
|
|
|
|
+ 42 + 42 + 42
|
|
|
|
});
|
|
|
|
|
|
|
|
// string case
|
|
|
|
(Ok("Bob") as Result<&str, &str>).unwrap_or("Alice");
|
|
|
|
|
|
|
|
// don't lint
|
|
|
|
match Ok(1) as Result<i32, &str> {
|
|
|
|
Ok(i) => i + 2,
|
|
|
|
Err(_) => 42,
|
|
|
|
};
|
|
|
|
match Ok(1) as Result<i32, &str> {
|
|
|
|
Ok(i) => i,
|
|
|
|
Err(_) => return,
|
|
|
|
};
|
|
|
|
for j in 0..4 {
|
|
|
|
match Ok(j) as Result<i32, &str> {
|
|
|
|
Ok(i) => i,
|
|
|
|
Err(_) => continue,
|
|
|
|
};
|
|
|
|
match Ok(j) as Result<i32, &str> {
|
|
|
|
Ok(i) => i,
|
|
|
|
Err(_) => break,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 04:49:08 -05:00
|
|
|
fn main() {}
|