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