]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/checked_unwrap/complex_conditionals.rs
New upstream version 1.74.1+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / checked_unwrap / complex_conditionals.rs
1 #![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
2 #![allow(
3 clippy::if_same_then_else,
4 clippy::branches_sharing_code,
5 clippy::unnecessary_literal_unwrap
6 )]
7
8 fn test_complex_conditions() {
9 let x: Result<(), ()> = Ok(());
10 let y: Result<(), ()> = Ok(());
11 if x.is_ok() && y.is_err() {
12 // unnecessary
13 x.unwrap();
14 //~^ ERROR: called `unwrap` on `x` after checking its variant with `is_ok`
15 // will panic
16 x.unwrap_err();
17 //~^ ERROR: this call to `unwrap_err()` will always panic
18 // will panic
19 y.unwrap();
20 //~^ ERROR: this call to `unwrap()` will always panic
21 // unnecessary
22 y.unwrap_err();
23 //~^ ERROR: called `unwrap_err` on `y` after checking its variant with `is_err`
24 } else {
25 // not statically determinable whether any of the following will always succeed or always fail:
26 x.unwrap();
27 x.unwrap_err();
28 y.unwrap();
29 y.unwrap_err();
30 }
31
32 if x.is_ok() || y.is_ok() {
33 // not statically determinable whether any of the following will always succeed or always fail:
34 x.unwrap();
35 y.unwrap();
36 } else {
37 // will panic
38 x.unwrap();
39 //~^ ERROR: this call to `unwrap()` will always panic
40 // unnecessary
41 x.unwrap_err();
42 //~^ ERROR: called `unwrap_err` on `x` after checking its variant with `is_ok`
43 // will panic
44 y.unwrap();
45 //~^ ERROR: this call to `unwrap()` will always panic
46 // unnecessary
47 y.unwrap_err();
48 //~^ ERROR: called `unwrap_err` on `y` after checking its variant with `is_ok`
49 }
50 let z: Result<(), ()> = Ok(());
51 if x.is_ok() && !(y.is_ok() || z.is_err()) {
52 // unnecessary
53 x.unwrap();
54 //~^ ERROR: called `unwrap` on `x` after checking its variant with `is_ok`
55 // will panic
56 x.unwrap_err();
57 //~^ ERROR: this call to `unwrap_err()` will always panic
58 // will panic
59 y.unwrap();
60 //~^ ERROR: this call to `unwrap()` will always panic
61 // unnecessary
62 y.unwrap_err();
63 //~^ ERROR: called `unwrap_err` on `y` after checking its variant with `is_ok`
64 // unnecessary
65 z.unwrap();
66 //~^ ERROR: called `unwrap` on `z` after checking its variant with `is_err`
67 // will panic
68 z.unwrap_err();
69 //~^ ERROR: this call to `unwrap_err()` will always panic
70 }
71 if x.is_ok() || !(y.is_ok() && z.is_err()) {
72 // not statically determinable whether any of the following will always succeed or always fail:
73 x.unwrap();
74 y.unwrap();
75 z.unwrap();
76 } else {
77 // will panic
78 x.unwrap();
79 //~^ ERROR: this call to `unwrap()` will always panic
80 // unnecessary
81 x.unwrap_err();
82 //~^ ERROR: called `unwrap_err` on `x` after checking its variant with `is_ok`
83 // unnecessary
84 y.unwrap();
85 //~^ ERROR: called `unwrap` on `y` after checking its variant with `is_ok`
86 // will panic
87 y.unwrap_err();
88 //~^ ERROR: this call to `unwrap_err()` will always panic
89 // will panic
90 z.unwrap();
91 //~^ ERROR: this call to `unwrap()` will always panic
92 // unnecessary
93 z.unwrap_err();
94 //~^ ERROR: called `unwrap_err` on `z` after checking its variant with `is_err`
95 }
96 }
97
98 fn main() {}