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