rust/tests/ui/question_mark.rs

121 lines
2.4 KiB
Rust
Raw Normal View History

2018-10-06 16:18:06 +00:00
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
2018-01-28 00:04:22 +00:00
fn some_func(a: Option<u32>) -> Option<u32> {
2018-12-09 22:26:16 +00:00
if a.is_none() {
return None;
}
2018-01-28 00:04:22 +00:00
2018-12-09 22:26:16 +00:00
a
2018-01-28 00:04:22 +00:00
}
2018-12-19 19:46:12 +00:00
fn some_other_func(a: Option<u32>) -> Option<u32> {
if a.is_none() {
return None;
} else {
return Some(0);
}
unreachable!()
}
2018-01-28 00:04:22 +00:00
pub enum SeemsOption<T> {
Some(T),
2018-12-09 22:26:16 +00:00
None,
2018-01-28 00:04:22 +00: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 22:26:16 +00:00
pub opt: Option<u32>,
2018-01-28 00:04:22 +00:00
}
impl CopyStruct {
#[rustfmt::skip]
2018-12-09 22:26:16 +00:00
pub fn func(&self) -> Option<u32> {
if (self.opt).is_none() {
return None;
}
2018-01-28 00:04:22 +00:00
if self.opt.is_none() {
return None
}
let _ = if self.opt.is_none() {
return None;
} else {
self.opt
};
2018-12-09 22:26:16 +00:00
self.opt
}
2018-01-28 00:04:22 +00: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())
}
}
2018-01-28 00:04:22 +00:00
fn main() {
2018-12-09 22:26:16 +00:00
some_func(Some(42));
some_func(None);
2018-01-28 00:04:22 +00: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();
move_struct.clone().mov_func_no_use();
2018-01-28 00:04:22 +00:00
let so = SeemsOption::Some(45);
returns_something_similar_to_option(so);
}