2020-10-06 09:49:08 +00:00
|
|
|
// run-rustfix
|
|
|
|
#![allow(dead_code)]
|
2020-11-17 16:01:22 +00:00
|
|
|
#![allow(unused_variables, clippy::unnecessary_wraps)]
|
2020-10-06 09:49:08 +00:00
|
|
|
|
2020-10-17 23:11:59 +00:00
|
|
|
fn option_unwrap_or() {
|
2020-10-06 09:49:08 +00:00
|
|
|
// int case
|
|
|
|
match Some(1) {
|
|
|
|
Some(i) => i,
|
|
|
|
None => 42,
|
|
|
|
};
|
|
|
|
|
2020-10-11 20:55:05 +00:00
|
|
|
// int case reversed
|
|
|
|
match Some(1) {
|
|
|
|
None => 42,
|
|
|
|
Some(i) => i,
|
|
|
|
};
|
|
|
|
|
2020-10-06 09:49:08 +00:00
|
|
|
// richer none expr
|
|
|
|
match Some(1) {
|
|
|
|
Some(i) => i,
|
|
|
|
None => 1 + 42,
|
|
|
|
};
|
|
|
|
|
|
|
|
// multiline case
|
2020-10-14 20:09:28 +00:00
|
|
|
#[rustfmt::skip]
|
2020-10-06 09:49:08 +00:00
|
|
|
match Some(1) {
|
|
|
|
Some(i) => i,
|
|
|
|
None => {
|
2020-10-14 20:09:28 +00:00
|
|
|
42 + 42
|
|
|
|
+ 42 + 42 + 42
|
|
|
|
+ 42 + 42 + 42
|
|
|
|
}
|
2020-10-06 09:49:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// string case
|
|
|
|
match Some("Bob") {
|
|
|
|
Some(i) => i,
|
|
|
|
None => "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 20:09:28 +00: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 09:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 23:11:59 +00:00
|
|
|
fn result_unwrap_or() {
|
|
|
|
// int case
|
2020-10-22 20:46:10 +00:00
|
|
|
match Ok::<i32, &str>(1) {
|
|
|
|
Ok(i) => i,
|
|
|
|
Err(_) => 42,
|
|
|
|
};
|
|
|
|
|
2020-10-22 21:39:59 +00:00
|
|
|
// int case, scrutinee is a binding
|
|
|
|
let a = Ok::<i32, &str>(1);
|
|
|
|
match a {
|
|
|
|
Ok(i) => i,
|
|
|
|
Err(_) => 42,
|
|
|
|
};
|
|
|
|
|
2020-10-24 09:35:05 +00:00
|
|
|
// int case, suggestion must surround Result expr with parenthesis
|
2020-10-17 23:11:59 +00:00
|
|
|
match Ok(1) as Result<i32, &str> {
|
|
|
|
Ok(i) => i,
|
|
|
|
Err(_) => 42,
|
|
|
|
};
|
|
|
|
|
2020-10-24 09:35:05 +00:00
|
|
|
// method call case, suggestion must not surround Result expr `s.method()` with parenthesis
|
|
|
|
struct S {}
|
|
|
|
impl S {
|
|
|
|
fn method(self) -> Option<i32> {
|
|
|
|
Some(42)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let s = S {};
|
|
|
|
match s.method() {
|
|
|
|
Some(i) => i,
|
|
|
|
None => 42,
|
|
|
|
};
|
|
|
|
|
2020-10-17 23:11:59 +00:00
|
|
|
// int case reversed
|
2020-10-22 20:46:10 +00:00
|
|
|
match Ok::<i32, &str>(1) {
|
2020-10-17 23:11:59 +00:00
|
|
|
Err(_) => 42,
|
|
|
|
Ok(i) => i,
|
|
|
|
};
|
|
|
|
|
|
|
|
// richer none expr
|
2020-10-22 20:46:10 +00:00
|
|
|
match Ok::<i32, &str>(1) {
|
2020-10-17 23:11:59 +00:00
|
|
|
Ok(i) => i,
|
|
|
|
Err(_) => 1 + 42,
|
|
|
|
};
|
|
|
|
|
|
|
|
// multiline case
|
|
|
|
#[rustfmt::skip]
|
2020-10-22 20:46:10 +00:00
|
|
|
match Ok::<i32, &str>(1) {
|
2020-10-17 23:11:59 +00:00
|
|
|
Ok(i) => i,
|
|
|
|
Err(_) => {
|
|
|
|
42 + 42
|
|
|
|
+ 42 + 42 + 42
|
|
|
|
+ 42 + 42 + 42
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// string case
|
2020-10-22 20:46:10 +00:00
|
|
|
match Ok::<&str, &str>("Bob") {
|
2020-10-17 23:11:59 +00:00
|
|
|
Ok(i) => i,
|
|
|
|
Err(_) => "Alice",
|
|
|
|
};
|
|
|
|
|
|
|
|
// don't lint
|
2020-10-22 20:46:10 +00:00
|
|
|
match Ok::<i32, &str>(1) {
|
2020-10-17 23:11:59 +00:00
|
|
|
Ok(i) => i + 2,
|
|
|
|
Err(_) => 42,
|
|
|
|
};
|
2020-10-22 20:46:10 +00:00
|
|
|
match Ok::<i32, &str>(1) {
|
2020-10-17 23:11:59 +00:00
|
|
|
Ok(i) => i,
|
|
|
|
Err(_) => return,
|
|
|
|
};
|
|
|
|
for j in 0..4 {
|
2020-10-22 20:46:10 +00:00
|
|
|
match Ok::<i32, &str>(j) {
|
2020-10-17 23:11:59 +00:00
|
|
|
Ok(i) => i,
|
|
|
|
Err(_) => continue,
|
|
|
|
};
|
2020-10-22 20:46:10 +00:00
|
|
|
match Ok::<i32, &str>(j) {
|
2020-10-17 23:11:59 +00:00
|
|
|
Ok(i) => i,
|
|
|
|
Err(_) => break,
|
|
|
|
};
|
|
|
|
}
|
2020-10-22 20:46:10 +00:00
|
|
|
|
|
|
|
// don't lint, Err value is used
|
|
|
|
match Ok::<&str, &str>("Alice") {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(s) => s,
|
|
|
|
};
|
|
|
|
// could lint, but unused_variables takes care of it
|
|
|
|
match Ok::<&str, &str>("Alice") {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(s) => "Bob",
|
|
|
|
};
|
2020-10-17 23:11:59 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 10:46:40 +00:00
|
|
|
// don't lint in const fn
|
2021-03-16 10:56:47 +00:00
|
|
|
const fn const_fn_option_unwrap_or() {
|
2021-03-16 10:46:40 +00:00
|
|
|
match Some(1) {
|
|
|
|
Some(s) => s,
|
|
|
|
None => 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-03-16 10:56:47 +00:00
|
|
|
const fn const_fn_result_unwrap_or() {
|
2021-03-16 10:46:40 +00:00
|
|
|
match Ok::<&str, &str>("Alice") {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(_) => "Bob",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-26 19:08:24 +00:00
|
|
|
mod issue6965 {
|
|
|
|
macro_rules! some_macro {
|
|
|
|
() => {
|
|
|
|
if 1 > 2 { Some(1) } else { None }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test() {
|
|
|
|
let _ = match some_macro!() {
|
|
|
|
Some(val) => val,
|
|
|
|
None => 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 09:49:08 +00:00
|
|
|
fn main() {}
|