2020-10-23 20:16:59 +00:00
|
|
|
#![allow(dead_code)]
|
2024-03-08 21:04:17 +00:00
|
|
|
#![allow(
|
|
|
|
unused_variables,
|
|
|
|
clippy::unnecessary_wraps,
|
|
|
|
clippy::unnecessary_literal_unwrap,
|
|
|
|
clippy::manual_unwrap_or_default
|
|
|
|
)]
|
2020-10-23 20:16:59 +00:00
|
|
|
|
2020-10-28 22:36:07 +00:00
|
|
|
fn option_unwrap_or() {
|
2020-10-23 20:16:59 +00:00
|
|
|
// int case
|
|
|
|
match Some(1) {
|
|
|
|
Some(i) => i,
|
|
|
|
None => 42,
|
|
|
|
};
|
|
|
|
|
|
|
|
// int case reversed
|
|
|
|
match Some(1) {
|
|
|
|
None => 42,
|
|
|
|
Some(i) => i,
|
|
|
|
};
|
|
|
|
|
|
|
|
// richer none expr
|
|
|
|
match Some(1) {
|
|
|
|
Some(i) => i,
|
|
|
|
None => 1 + 42,
|
|
|
|
};
|
|
|
|
|
|
|
|
// multiline case
|
|
|
|
#[rustfmt::skip]
|
|
|
|
match Some(1) {
|
|
|
|
Some(i) => i,
|
|
|
|
None => {
|
|
|
|
42 + 42
|
|
|
|
+ 42 + 42 + 42
|
|
|
|
+ 42 + 42 + 42
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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"),
|
|
|
|
};
|
2024-06-08 13:34:59 +00:00
|
|
|
|
|
|
|
if let Some(x) = Some(1) {
|
|
|
|
x
|
|
|
|
} else {
|
|
|
|
42
|
|
|
|
};
|
|
|
|
|
|
|
|
//don't lint
|
|
|
|
if let Some(x) = Some(1) {
|
|
|
|
x + 1
|
|
|
|
} else {
|
|
|
|
42
|
|
|
|
};
|
|
|
|
if let Some(x) = Some(1) {
|
|
|
|
x
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
for j in 0..4 {
|
|
|
|
if let Some(x) = Some(j) {
|
|
|
|
x
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
if let Some(x) = Some(j) {
|
|
|
|
x
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
2020-10-23 20:16:59 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 22:36:07 +00:00
|
|
|
fn result_unwrap_or() {
|
|
|
|
// int case
|
|
|
|
match Ok::<i32, &str>(1) {
|
|
|
|
Ok(i) => i,
|
|
|
|
Err(_) => 42,
|
|
|
|
};
|
|
|
|
|
|
|
|
// int case, scrutinee is a binding
|
|
|
|
let a = Ok::<i32, &str>(1);
|
|
|
|
match a {
|
|
|
|
Ok(i) => i,
|
|
|
|
Err(_) => 42,
|
|
|
|
};
|
|
|
|
|
2021-10-17 10:04:01 +00:00
|
|
|
// int case, suggestion must surround Result expr with parentheses
|
2020-10-28 22:36:07 +00:00
|
|
|
match Ok(1) as Result<i32, &str> {
|
|
|
|
Ok(i) => i,
|
|
|
|
Err(_) => 42,
|
|
|
|
};
|
|
|
|
|
2021-10-17 10:04:01 +00:00
|
|
|
// method call case, suggestion must not surround Result expr `s.method()` with parentheses
|
2022-03-27 12:41:09 +00:00
|
|
|
struct S;
|
2020-10-28 22:36:07 +00:00
|
|
|
impl S {
|
|
|
|
fn method(self) -> Option<i32> {
|
|
|
|
Some(42)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let s = S {};
|
|
|
|
match s.method() {
|
|
|
|
Some(i) => i,
|
|
|
|
None => 42,
|
|
|
|
};
|
|
|
|
|
|
|
|
// int case reversed
|
|
|
|
match Ok::<i32, &str>(1) {
|
|
|
|
Err(_) => 42,
|
|
|
|
Ok(i) => i,
|
|
|
|
};
|
|
|
|
|
|
|
|
// richer none expr
|
|
|
|
match Ok::<i32, &str>(1) {
|
|
|
|
Ok(i) => i,
|
|
|
|
Err(_) => 1 + 42,
|
|
|
|
};
|
|
|
|
|
|
|
|
// multiline case
|
|
|
|
#[rustfmt::skip]
|
|
|
|
match Ok::<i32, &str>(1) {
|
|
|
|
Ok(i) => i,
|
|
|
|
Err(_) => {
|
|
|
|
42 + 42
|
|
|
|
+ 42 + 42 + 42
|
|
|
|
+ 42 + 42 + 42
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// string case
|
|
|
|
match Ok::<&str, &str>("Bob") {
|
|
|
|
Ok(i) => i,
|
|
|
|
Err(_) => "Alice",
|
|
|
|
};
|
|
|
|
|
|
|
|
// don't lint
|
|
|
|
match Ok::<i32, &str>(1) {
|
|
|
|
Ok(i) => i + 2,
|
|
|
|
Err(_) => 42,
|
|
|
|
};
|
|
|
|
match Ok::<i32, &str>(1) {
|
|
|
|
Ok(i) => i,
|
|
|
|
Err(_) => return,
|
|
|
|
};
|
|
|
|
for j in 0..4 {
|
|
|
|
match Ok::<i32, &str>(j) {
|
|
|
|
Ok(i) => i,
|
|
|
|
Err(_) => continue,
|
|
|
|
};
|
|
|
|
match Ok::<i32, &str>(j) {
|
|
|
|
Ok(i) => i,
|
|
|
|
Err(_) => break,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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",
|
|
|
|
};
|
2024-06-08 13:34:59 +00:00
|
|
|
|
|
|
|
if let Ok(x) = Ok::<i32, i32>(1) {
|
|
|
|
x
|
|
|
|
} else {
|
|
|
|
42
|
|
|
|
};
|
|
|
|
|
|
|
|
//don't lint
|
|
|
|
if let Ok(x) = Ok::<i32, i32>(1) {
|
|
|
|
x + 1
|
|
|
|
} else {
|
|
|
|
42
|
|
|
|
};
|
|
|
|
if let Ok(x) = Ok::<i32, i32>(1) {
|
|
|
|
x
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
for j in 0..4 {
|
|
|
|
if let Ok(x) = Ok::<i32, i32>(j) {
|
|
|
|
x
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
if let Ok(x) = Ok::<i32, i32>(j) {
|
|
|
|
x
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
2020-10-28 22:36:07 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 18:29:11 +00:00
|
|
|
// don't lint in const fn
|
|
|
|
const fn const_fn_option_unwrap_or() {
|
|
|
|
match Some(1) {
|
|
|
|
Some(s) => s,
|
|
|
|
None => 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn const_fn_result_unwrap_or() {
|
|
|
|
match Ok::<&str, &str>("Alice") {
|
|
|
|
Ok(s) => s,
|
|
|
|
Err(_) => "Bob",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-27 14:55:11 +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,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-20 10:30:31 +00:00
|
|
|
use std::rc::Rc;
|
|
|
|
fn format_name(name: Option<&Rc<str>>) -> &str {
|
|
|
|
match name {
|
|
|
|
None => "<anon>",
|
|
|
|
Some(name) => name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn implicit_deref_ref() {
|
|
|
|
let _: &str = match Some(&"bye") {
|
|
|
|
None => "hi",
|
|
|
|
Some(s) => s,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-07-07 01:11:32 +00:00
|
|
|
mod issue_13018 {
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
type RefName = i32;
|
|
|
|
pub fn get(index: &HashMap<usize, Vec<RefName>>, id: usize) -> &[RefName] {
|
2024-07-07 13:41:55 +00:00
|
|
|
if let Some(names) = index.get(&id) { names } else { &[] }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_match(index: &HashMap<usize, Vec<RefName>>, id: usize) -> &[RefName] {
|
|
|
|
match index.get(&id) {
|
|
|
|
Some(names) => names,
|
|
|
|
None => &[],
|
2024-07-07 01:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 20:16:59 +00:00
|
|
|
fn main() {}
|