rust/tests/ui/question_mark.rs

195 lines
3.4 KiB
Rust
Raw Normal View History

2020-03-04 02:21:07 -06:00
// run-rustfix
#![allow(unreachable_code)]
2020-11-17 10:01:22 -06:00
#![allow(clippy::unnecessary_wraps)]
2020-03-04 02:21:07 -06:00
use std::env;
use std::path::PathBuf;
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
}
pub struct CopyStruct {
2018-12-09 16:26:16 -06:00
pub opt: Option<u32>,
2018-01-27 18:04:22 -06:00
}
impl CopyStruct {
#[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
if self.opt.is_none() {
return None
}
let _ = if self.opt.is_none() {
return None;
} else {
self.opt
};
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
}
#[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())
}
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 {
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 {
v
} else {
return None;
};
Some(v)
}
}
fn func() -> Option<i32> {
fn f() -> Option<String> {
Some(String::new())
}
if f().is_none() {
return None;
}
Some(0)
}
fn func_returning_result() -> Result<i32, String> {
Ok(1)
}
fn result_func(x: Result<i32, String>) -> Result<i32, String> {
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 {
return Err("some error".to_string());
};
// issue #7859
// no warning
let _ = if let Ok(x) = func_returning_result() {
x
} else {
return Err("some error".to_string());
};
if func_returning_result().is_err() {
return func_returning_result();
}
Ok(y)
}
2018-01-27 18:04:22 -06:00
fn main() {
2018-12-09 16:26:16 -06:00
some_func(Some(42));
some_func(None);
2020-03-04 02:21:07 -06:00
some_other_func(Some(42));
2018-01-27 18:04:22 -06:00
let copy_struct = CopyStruct { opt: Some(54) };
copy_struct.func();
let move_struct = MoveStruct {
opt: Some(vec![42, 1337]),
};
move_struct.ref_func();
move_struct.clone().mov_func_reuse();
2019-09-16 10:50:36 -05:00
move_struct.mov_func_no_use();
2018-01-27 18:04:22 -06:00
let so = SeemsOption::Some(45);
returns_something_similar_to_option(so);
func();
let _ = result_func(Ok(42));
2018-01-27 18:04:22 -06:00
}