]> git.lizzy.rs Git - rust.git/blob - tests/ui/checked_unwrap/simple_conditionals.rs
Auto merge of #8374 - Alexendoo:bless-revisions, r=camsteffen
[rust.git] / tests / ui / checked_unwrap / simple_conditionals.rs
1 #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
2 #![allow(clippy::if_same_then_else, clippy::branches_sharing_code)]
3
4 macro_rules! m {
5     ($a:expr) => {
6         if $a.is_some() {
7             $a.unwrap(); // unnecessary
8         }
9     };
10 }
11
12 macro_rules! checks_in_param {
13     ($a:expr, $b:expr) => {
14         if $a {
15             $b;
16         }
17     };
18 }
19
20 macro_rules! checks_unwrap {
21     ($a:expr, $b:expr) => {
22         if $a.is_some() {
23             $b;
24         }
25     };
26 }
27
28 macro_rules! checks_some {
29     ($a:expr, $b:expr) => {
30         if $a {
31             $b.unwrap();
32         }
33     };
34 }
35
36 fn main() {
37     let x = Some(());
38     if x.is_some() {
39         x.unwrap(); // unnecessary
40         x.expect("an error message"); // unnecessary
41     } else {
42         x.unwrap(); // will panic
43         x.expect("an error message"); // will panic
44     }
45     if x.is_none() {
46         x.unwrap(); // will panic
47     } else {
48         x.unwrap(); // unnecessary
49     }
50     m!(x);
51     checks_in_param!(x.is_some(), x.unwrap()); // ok
52     checks_unwrap!(x, x.unwrap()); // ok
53     checks_some!(x.is_some(), x); // ok
54     let mut x: Result<(), ()> = Ok(());
55     if x.is_ok() {
56         x.unwrap(); // unnecessary
57         x.expect("an error message"); // unnecessary
58         x.unwrap_err(); // will panic
59     } else {
60         x.unwrap(); // will panic
61         x.expect("an error message"); // will panic
62         x.unwrap_err(); // unnecessary
63     }
64     if x.is_err() {
65         x.unwrap(); // will panic
66         x.unwrap_err(); // unnecessary
67     } else {
68         x.unwrap(); // unnecessary
69         x.unwrap_err(); // will panic
70     }
71     if x.is_ok() {
72         x = Err(());
73         // not unnecessary because of mutation of x
74         // it will always panic but the lint is not smart enough to see this (it only
75         // checks if conditions).
76         x.unwrap();
77     } else {
78         x = Ok(());
79         // not unnecessary because of mutation of x
80         // it will always panic but the lint is not smart enough to see this (it
81         // only checks if conditions).
82         x.unwrap_err();
83     }
84
85     assert!(x.is_ok(), "{:?}", x.unwrap_err()); // ok, it's a common test pattern
86 }