1 #![deny(panicking_unwrap, unnecessary_unwrap)]
2 #![allow(if_same_then_else)]
7 x.unwrap(); // unnecessary
9 x.unwrap(); // will panic
12 x.unwrap(); // will panic
14 x.unwrap(); // unnecessary
16 let mut x: Result<(), ()> = Ok(());
18 x.unwrap(); // unnecessary
19 x.unwrap_err(); // will panic
21 x.unwrap(); // will panic
22 x.unwrap_err(); // unnecessary
25 x.unwrap(); // will panic
26 x.unwrap_err(); // unnecessary
28 x.unwrap(); // unnecessary
29 x.unwrap_err(); // will panic
33 x.unwrap(); // not unnecessary because of mutation of x
34 // it will always panic but the lint is not smart enough to see this (it only checks if conditions).
37 x.unwrap_err(); // not unnecessary because of mutation of x
38 // it will always panic but the lint is not smart enough to see this (it only checks if conditions).
42 fn test_complex_conditions() {
43 let x: Result<(), ()> = Ok(());
44 let y: Result<(), ()> = Ok(());
45 if x.is_ok() && y.is_err() {
46 x.unwrap(); // unnecessary
47 x.unwrap_err(); // will panic
48 y.unwrap(); // will panic
49 y.unwrap_err(); // unnecessary
51 // not statically determinable whether any of the following will always succeed or always fail:
58 if x.is_ok() || y.is_ok() {
59 // not statically determinable whether any of the following will always succeed or always fail:
63 x.unwrap(); // will panic
64 x.unwrap_err(); // unnecessary
65 y.unwrap(); // will panic
66 y.unwrap_err(); // unnecessary
68 let z: Result<(), ()> = Ok(());
69 if x.is_ok() && !(y.is_ok() || z.is_err()) {
70 x.unwrap(); // unnecessary
71 x.unwrap_err(); // will panic
72 y.unwrap(); // will panic
73 y.unwrap_err(); // unnecessary
74 z.unwrap(); // unnecessary
75 z.unwrap_err(); // will panic
77 if x.is_ok() || !(y.is_ok() && z.is_err()) {
78 // not statically determinable whether any of the following will always succeed or always fail:
83 x.unwrap(); // will panic
84 x.unwrap_err(); // unnecessary
85 y.unwrap(); // unnecessary
86 y.unwrap_err(); // will panic
87 z.unwrap(); // will panic
88 z.unwrap_err(); // unnecessary
96 x.unwrap(); // unnecessary
98 x.unwrap(); // will panic