]> git.lizzy.rs Git - rust.git/blobdiff - tests/ui/question_mark.rs
iterate List by value
[rust.git] / tests / ui / question_mark.rs
index b1edec32eeeb0740c18485661304f6bcad90c036..1d0ee82b4f7787b5e56445c334b2115066804d67 100644 (file)
@@ -1,11 +1,5 @@
-// 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.
+// run-rustfix
+#![allow(unreachable_code)]
 
 fn some_func(a: Option<u32>) -> Option<u32> {
     if a.is_none() {
@@ -15,6 +9,15 @@ fn some_func(a: Option<u32>) -> Option<u32> {
     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,
@@ -37,11 +40,11 @@ fn returns_something_similar_to_option(a: SeemsOption<u32>) -> SeemsOption<u32>
     a
 }
 
-pub struct SomeStruct {
+pub struct CopyStruct {
     pub opt: Option<u32>,
 }
 
-impl SomeStruct {
+impl CopyStruct {
     #[rustfmt::skip]
     pub fn func(&self) -> Option<u32> {
         if (self.opt).is_none() {
@@ -58,17 +61,95 @@ pub fn func(&self) -> Option<u32> {
             self.opt
         };
 
+        let _ = if let Some(x) = self.opt {
+            x
+        } else {
+            return None;
+        };
+
         self.opt
     }
 }
 
+#[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>> {
+        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>> {
+        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 main() {
     some_func(Some(42));
     some_func(None);
+    some_other_func(Some(42));
+
+    let copy_struct = CopyStruct { opt: Some(54) };
+    copy_struct.func();
 
-    let some_struct = SomeStruct { opt: Some(54) };
-    some_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();
 }