]> git.lizzy.rs Git - rust.git/blob - tests/ui/checked_unwrap.rs
Auto merge of #3603 - xfix:random-state-lint, r=phansch
[rust.git] / tests / ui / checked_unwrap.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
11 #![allow(clippy::if_same_then_else)]
12
13 fn main() {
14     let x = Some(());
15     if x.is_some() {
16         x.unwrap(); // unnecessary
17     } else {
18         x.unwrap(); // will panic
19     }
20     if x.is_none() {
21         x.unwrap(); // will panic
22     } else {
23         x.unwrap(); // unnecessary
24     }
25     let mut x: Result<(), ()> = Ok(());
26     if x.is_ok() {
27         x.unwrap(); // unnecessary
28         x.unwrap_err(); // will panic
29     } else {
30         x.unwrap(); // will panic
31         x.unwrap_err(); // unnecessary
32     }
33     if x.is_err() {
34         x.unwrap(); // will panic
35         x.unwrap_err(); // unnecessary
36     } else {
37         x.unwrap(); // unnecessary
38         x.unwrap_err(); // will panic
39     }
40     if x.is_ok() {
41         x = Err(());
42         x.unwrap(); // not unnecessary because of mutation of x
43                     // it will always panic but the lint is not smart enough to see this (it only checks if conditions).
44     } else {
45         x = Ok(());
46         x.unwrap_err(); // not unnecessary because of mutation of x
47                         // it will always panic but the lint is not smart enough to see this (it only checks if conditions).
48     }
49 }
50
51 fn test_complex_conditions() {
52     let x: Result<(), ()> = Ok(());
53     let y: Result<(), ()> = Ok(());
54     if x.is_ok() && y.is_err() {
55         x.unwrap(); // unnecessary
56         x.unwrap_err(); // will panic
57         y.unwrap(); // will panic
58         y.unwrap_err(); // unnecessary
59     } else {
60         // not statically determinable whether any of the following will always succeed or always fail:
61         x.unwrap();
62         x.unwrap_err();
63         y.unwrap();
64         y.unwrap_err();
65     }
66
67     if x.is_ok() || y.is_ok() {
68         // not statically determinable whether any of the following will always succeed or always fail:
69         x.unwrap();
70         y.unwrap();
71     } else {
72         x.unwrap(); // will panic
73         x.unwrap_err(); // unnecessary
74         y.unwrap(); // will panic
75         y.unwrap_err(); // unnecessary
76     }
77     let z: Result<(), ()> = Ok(());
78     if x.is_ok() && !(y.is_ok() || z.is_err()) {
79         x.unwrap(); // unnecessary
80         x.unwrap_err(); // will panic
81         y.unwrap(); // will panic
82         y.unwrap_err(); // unnecessary
83         z.unwrap(); // unnecessary
84         z.unwrap_err(); // will panic
85     }
86     if x.is_ok() || !(y.is_ok() && z.is_err()) {
87         // not statically determinable whether any of the following will always succeed or always fail:
88         x.unwrap();
89         y.unwrap();
90         z.unwrap();
91     } else {
92         x.unwrap(); // will panic
93         x.unwrap_err(); // unnecessary
94         y.unwrap(); // unnecessary
95         y.unwrap_err(); // will panic
96         z.unwrap(); // will panic
97         z.unwrap_err(); // unnecessary
98     }
99 }
100
101 fn test_nested() {
102     fn nested() {
103         let x = Some(());
104         if x.is_some() {
105             x.unwrap(); // unnecessary
106         } else {
107             x.unwrap(); // will panic
108         }
109     }
110 }