]> git.lizzy.rs Git - rust.git/blob - tests/ui/checked_unwrap.rs
Merge branch 'master' into add-lints-aseert-checks
[rust.git] / tests / ui / checked_unwrap.rs
1 #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
2 #![allow(clippy::if_same_then_else)]
3
4 fn main() {
5     let x = Some(());
6     if x.is_some() {
7         x.unwrap(); // unnecessary
8     } else {
9         x.unwrap(); // will panic
10     }
11     if x.is_none() {
12         x.unwrap(); // will panic
13     } else {
14         x.unwrap(); // unnecessary
15     }
16     let mut x: Result<(), ()> = Ok(());
17     if x.is_ok() {
18         x.unwrap(); // unnecessary
19         x.unwrap_err(); // will panic
20     } else {
21         x.unwrap(); // will panic
22         x.unwrap_err(); // unnecessary
23     }
24     if x.is_err() {
25         x.unwrap(); // will panic
26         x.unwrap_err(); // unnecessary
27     } else {
28         x.unwrap(); // unnecessary
29         x.unwrap_err(); // will panic
30     }
31     if x.is_ok() {
32         x = Err(());
33         x.unwrap(); // not unnecessary because of mutation of x
34                     // it will always panic but the lint is not smart enough to see this (it only checks if conditions).
35     } else {
36         x = Ok(());
37         x.unwrap_err(); // not unnecessary because of mutation of x
38                         // it will always panic but the lint is not smart enough to see this (it only checks if conditions).
39     }
40 }
41
42 fn test_complex_conditions() {
43     let x: Result<(), ()> = Ok(());
44     let y: Result<(), ()> = Ok(());
45     if x.is_ok() && y.is_err() {
46         x.unwrap(); // unnecessary
47         x.unwrap_err(); // will panic
48         y.unwrap(); // will panic
49         y.unwrap_err(); // unnecessary
50     } else {
51         // not statically determinable whether any of the following will always succeed or always fail:
52         x.unwrap();
53         x.unwrap_err();
54         y.unwrap();
55         y.unwrap_err();
56     }
57
58     if x.is_ok() || y.is_ok() {
59         // not statically determinable whether any of the following will always succeed or always fail:
60         x.unwrap();
61         y.unwrap();
62     } else {
63         x.unwrap(); // will panic
64         x.unwrap_err(); // unnecessary
65         y.unwrap(); // will panic
66         y.unwrap_err(); // unnecessary
67     }
68     let z: Result<(), ()> = Ok(());
69     if x.is_ok() && !(y.is_ok() || z.is_err()) {
70         x.unwrap(); // unnecessary
71         x.unwrap_err(); // will panic
72         y.unwrap(); // will panic
73         y.unwrap_err(); // unnecessary
74         z.unwrap(); // unnecessary
75         z.unwrap_err(); // will panic
76     }
77     if x.is_ok() || !(y.is_ok() && z.is_err()) {
78         // not statically determinable whether any of the following will always succeed or always fail:
79         x.unwrap();
80         y.unwrap();
81         z.unwrap();
82     } else {
83         x.unwrap(); // will panic
84         x.unwrap_err(); // unnecessary
85         y.unwrap(); // unnecessary
86         y.unwrap_err(); // will panic
87         z.unwrap(); // will panic
88         z.unwrap_err(); // unnecessary
89     }
90 }
91
92 fn test_nested() {
93     fn nested() {
94         let x = Some(());
95         if x.is_some() {
96             x.unwrap(); // unnecessary
97         } else {
98             x.unwrap(); // will panic
99         }
100     }
101 }