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