2020-03-04 02:21:07 -06:00
|
|
|
// run-rustfix
|
|
|
|
#![allow(unreachable_code)]
|
2022-07-08 11:12:00 -05:00
|
|
|
#![allow(dead_code)]
|
2020-11-17 10:01:22 -06:00
|
|
|
#![allow(clippy::unnecessary_wraps)]
|
2020-03-04 02:21:07 -06:00
|
|
|
|
2018-01-27 18:04:22 -06:00
|
|
|
fn some_func(a: Option<u32>) -> Option<u32> {
|
2018-12-09 16:26:16 -06:00
|
|
|
if a.is_none() {
|
|
|
|
return None;
|
|
|
|
}
|
2018-01-27 18:04:22 -06:00
|
|
|
|
2018-12-09 16:26:16 -06:00
|
|
|
a
|
2018-01-27 18:04:22 -06:00
|
|
|
}
|
|
|
|
|
2018-12-19 13:46:12 -06:00
|
|
|
fn some_other_func(a: Option<u32>) -> Option<u32> {
|
|
|
|
if a.is_none() {
|
|
|
|
return None;
|
|
|
|
} else {
|
2018-12-28 13:52:46 -06:00
|
|
|
return Some(0);
|
2018-12-19 13:46:12 -06:00
|
|
|
}
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
|
2018-01-27 18:04:22 -06:00
|
|
|
pub enum SeemsOption<T> {
|
|
|
|
Some(T),
|
2018-12-09 16:26:16 -06:00
|
|
|
None,
|
2018-01-27 18:04:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> SeemsOption<T> {
|
|
|
|
pub fn is_none(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
SeemsOption::None => true,
|
|
|
|
SeemsOption::Some(_) => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn returns_something_similar_to_option(a: SeemsOption<u32>) -> SeemsOption<u32> {
|
|
|
|
if a.is_none() {
|
|
|
|
return SeemsOption::None;
|
|
|
|
}
|
|
|
|
|
|
|
|
a
|
|
|
|
}
|
|
|
|
|
2018-12-18 06:55:04 -06:00
|
|
|
pub struct CopyStruct {
|
2018-12-09 16:26:16 -06:00
|
|
|
pub opt: Option<u32>,
|
2018-01-27 18:04:22 -06:00
|
|
|
}
|
|
|
|
|
2018-12-18 06:55:04 -06:00
|
|
|
impl CopyStruct {
|
2018-12-12 02:46:52 -06:00
|
|
|
#[rustfmt::skip]
|
2018-12-09 16:26:16 -06:00
|
|
|
pub fn func(&self) -> Option<u32> {
|
|
|
|
if (self.opt).is_none() {
|
|
|
|
return None;
|
|
|
|
}
|
2018-01-27 18:04:22 -06:00
|
|
|
|
2018-12-12 02:46:52 -06:00
|
|
|
if self.opt.is_none() {
|
|
|
|
return None
|
|
|
|
}
|
|
|
|
|
|
|
|
let _ = if self.opt.is_none() {
|
|
|
|
return None;
|
|
|
|
} else {
|
|
|
|
self.opt
|
|
|
|
};
|
|
|
|
|
2020-03-04 01:59:16 -06:00
|
|
|
let _ = if let Some(x) = self.opt {
|
|
|
|
x
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
|
2018-12-09 16:26:16 -06:00
|
|
|
self.opt
|
|
|
|
}
|
2018-01-27 18:04:22 -06:00
|
|
|
}
|
|
|
|
|
2018-12-18 06:55:04 -06:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct MoveStruct {
|
|
|
|
pub opt: Option<Vec<u32>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MoveStruct {
|
|
|
|
pub fn ref_func(&self) -> Option<Vec<u32>> {
|
|
|
|
if self.opt.is_none() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.opt.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mov_func_reuse(self) -> Option<Vec<u32>> {
|
|
|
|
if self.opt.is_none() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.opt
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mov_func_no_use(self) -> Option<Vec<u32>> {
|
|
|
|
if self.opt.is_none() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(Vec::new())
|
|
|
|
}
|
2020-03-04 01:59:16 -06:00
|
|
|
|
|
|
|
pub fn if_let_ref_func(self) -> Option<Vec<u32>> {
|
2020-03-04 02:21:07 -06:00
|
|
|
let v: &Vec<_> = if let Some(ref v) = self.opt {
|
2020-03-04 01:59:16 -06:00
|
|
|
v
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(v.clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn if_let_mov_func(self) -> Option<Vec<u32>> {
|
2020-03-04 02:21:07 -06:00
|
|
|
let v = if let Some(v) = self.opt {
|
2020-03-04 01:59:16 -06:00
|
|
|
v
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(v)
|
|
|
|
}
|
2018-12-18 06:55:04 -06:00
|
|
|
}
|
|
|
|
|
2020-04-16 23:09:02 -05:00
|
|
|
fn func() -> Option<i32> {
|
|
|
|
fn f() -> Option<String> {
|
|
|
|
Some(String::new())
|
|
|
|
}
|
|
|
|
|
|
|
|
if f().is_none() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(0)
|
|
|
|
}
|
|
|
|
|
2021-10-22 01:11:04 -05:00
|
|
|
fn func_returning_result() -> Result<i32, i32> {
|
2021-10-22 00:36:13 -05:00
|
|
|
Ok(1)
|
|
|
|
}
|
|
|
|
|
2021-10-22 01:11:04 -05:00
|
|
|
fn result_func(x: Result<i32, i32>) -> Result<i32, i32> {
|
2021-10-19 05:27:37 -05:00
|
|
|
let _ = if let Ok(x) = x { x } else { return x };
|
|
|
|
|
|
|
|
if x.is_err() {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No warning
|
|
|
|
let y = if let Ok(x) = x {
|
|
|
|
x
|
|
|
|
} else {
|
2021-10-22 01:11:04 -05:00
|
|
|
return Err(0);
|
2021-10-19 05:27:37 -05:00
|
|
|
};
|
|
|
|
|
2021-10-22 00:36:13 -05:00
|
|
|
// issue #7859
|
|
|
|
// no warning
|
|
|
|
let _ = if let Ok(x) = func_returning_result() {
|
|
|
|
x
|
|
|
|
} else {
|
2021-10-22 01:11:04 -05:00
|
|
|
return Err(0);
|
2021-10-22 00:36:13 -05:00
|
|
|
};
|
|
|
|
|
2021-10-22 01:11:04 -05:00
|
|
|
// no warning
|
2021-10-22 00:36:13 -05:00
|
|
|
if func_returning_result().is_err() {
|
|
|
|
return func_returning_result();
|
|
|
|
}
|
|
|
|
|
2021-10-19 05:27:37 -05:00
|
|
|
Ok(y)
|
|
|
|
}
|
|
|
|
|
2021-12-06 03:18:17 -06:00
|
|
|
// see issue #8019
|
|
|
|
pub enum NotOption {
|
|
|
|
None,
|
|
|
|
First,
|
|
|
|
AfterFirst,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn obj(_: i32) -> Result<(), NotOption> {
|
|
|
|
Err(NotOption::First)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn f() -> NotOption {
|
|
|
|
if obj(2).is_err() {
|
|
|
|
return NotOption::None;
|
|
|
|
}
|
|
|
|
NotOption::First
|
|
|
|
}
|
|
|
|
|
2022-07-08 11:12:00 -05:00
|
|
|
fn do_something() {}
|
2018-01-27 18:04:22 -06:00
|
|
|
|
2022-07-08 11:12:00 -05:00
|
|
|
fn err_immediate_return() -> Result<i32, i32> {
|
|
|
|
if let Err(err) = func_returning_result() {
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
Ok(1)
|
|
|
|
}
|
2018-12-18 06:55:04 -06:00
|
|
|
|
2022-07-08 11:12:00 -05:00
|
|
|
fn err_immediate_return_and_do_something() -> Result<i32, i32> {
|
|
|
|
if let Err(err) = func_returning_result() {
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
do_something();
|
|
|
|
Ok(1)
|
|
|
|
}
|
2018-01-27 18:04:22 -06:00
|
|
|
|
2022-07-08 11:12:00 -05:00
|
|
|
// No warning
|
|
|
|
fn no_immediate_return() -> Result<i32, i32> {
|
|
|
|
if let Err(err) = func_returning_result() {
|
|
|
|
do_something();
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
Ok(1)
|
|
|
|
}
|
2020-04-16 23:09:02 -05:00
|
|
|
|
2022-07-08 11:12:00 -05:00
|
|
|
// No warning
|
|
|
|
fn mixed_result_and_option() -> Option<i32> {
|
|
|
|
if let Err(err) = func_returning_result() {
|
|
|
|
return Some(err);
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2021-10-19 05:27:37 -05:00
|
|
|
|
2022-07-08 11:12:00 -05:00
|
|
|
// No warning
|
|
|
|
fn else_if_check() -> Result<i32, i32> {
|
|
|
|
if true {
|
|
|
|
Ok(1)
|
|
|
|
} else if let Err(e) = func_returning_result() {
|
|
|
|
Err(e)
|
|
|
|
} else {
|
|
|
|
Err(-1)
|
|
|
|
}
|
2018-01-27 18:04:22 -06:00
|
|
|
}
|
2022-07-08 11:12:00 -05:00
|
|
|
|
|
|
|
// No warning
|
|
|
|
#[allow(clippy::manual_map)]
|
|
|
|
#[rustfmt::skip]
|
|
|
|
fn option_map() -> Option<bool> {
|
|
|
|
if let Some(a) = Some(false) {
|
|
|
|
Some(!a)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {}
|