rust/tests/ui/question_mark.fixed

127 lines
2.1 KiB
Rust
Raw Normal View History

2020-03-04 02:21:07 -06:00
// run-rustfix
#![allow(unreachable_code)]
#![allow(clippy::unnecessary_wraps)]
2020-03-04 02:21:07 -06:00
fn some_func(a: Option<u32>) -> Option<u32> {
a?;
a
}
fn some_other_func(a: Option<u32>) -> Option<u32> {
if a.is_none() {
return None;
} else {
return Some(0);
}
unreachable!()
}
pub enum SeemsOption<T> {
Some(T),
None,
}
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 {
pub opt: Option<u32>,
}
impl CopyStruct {
#[rustfmt::skip]
pub fn func(&self) -> Option<u32> {
(self.opt)?;
self.opt?;
let _ = Some(self.opt?);
let _ = self.opt?;
self.opt
}
}
#[derive(Clone)]
pub struct MoveStruct {
pub opt: Option<Vec<u32>>,
}
impl MoveStruct {
pub fn ref_func(&self) -> Option<Vec<u32>> {
self.opt.as_ref()?;
self.opt.clone()
}
pub fn mov_func_reuse(self) -> Option<Vec<u32>> {
self.opt.as_ref()?;
self.opt
}
pub fn mov_func_no_use(self) -> Option<Vec<u32>> {
self.opt.as_ref()?;
Some(Vec::new())
}
pub fn if_let_ref_func(self) -> Option<Vec<u32>> {
let v: &Vec<_> = self.opt.as_ref()?;
Some(v.clone())
}
pub fn if_let_mov_func(self) -> Option<Vec<u32>> {
let v = self.opt?;
Some(v)
}
}
fn func() -> Option<i32> {
fn f() -> Option<String> {
Some(String::new())
}
f()?;
Some(0)
}
2020-03-04 02:21:07 -06:00
fn main() {
some_func(Some(42));
some_func(None);
some_other_func(Some(42));
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();
move_struct.mov_func_no_use();
let so = SeemsOption::Some(45);
returns_something_similar_to_option(so);
func();
2020-03-04 02:21:07 -06:00
}