|
1 |
| -#![deny(unnecessary_unwrap)] |
| 1 | +#![deny(panicking_unwrap, unnecessary_unwrap)] |
| 2 | +#![allow(if_same_then_else)] |
2 | 3 |
|
3 | 4 | fn main() {
|
4 | 5 | let x = Some(());
|
5 | 6 | if x.is_some() {
|
6 |
| - x.unwrap(); |
| 7 | + x.unwrap(); // unnecessary |
| 8 | + } else { |
| 9 | + x.unwrap(); // will panic |
7 | 10 | }
|
8 | 11 | if x.is_none() {
|
9 |
| - // nothing to do here |
| 12 | + x.unwrap(); // will panic |
10 | 13 | } else {
|
11 |
| - x.unwrap(); |
| 14 | + x.unwrap(); // unnecessary |
12 | 15 | }
|
13 | 16 | let mut x: Result<(), ()> = Ok(());
|
14 | 17 | if x.is_ok() {
|
15 |
| - x.unwrap(); |
| 18 | + x.unwrap(); // unnecessary |
| 19 | + x.unwrap_err(); // will panic |
16 | 20 | } else {
|
17 |
| - x.unwrap_err(); |
| 21 | + x.unwrap(); // will panic |
| 22 | + x.unwrap_err(); // unnecessary |
18 | 23 | }
|
19 | 24 | if x.is_err() {
|
20 |
| - x.unwrap_err(); |
| 25 | + x.unwrap(); // will panic |
| 26 | + x.unwrap_err(); // unnecessary |
21 | 27 | } else {
|
22 |
| - x.unwrap(); |
| 28 | + x.unwrap(); // unnecessary |
| 29 | + x.unwrap_err(); // will panic |
23 | 30 | }
|
24 | 31 | if x.is_ok() {
|
25 | 32 | x = Err(());
|
26 |
| - x.unwrap(); |
| 33 | + x.unwrap(); // not unnecessary because of mutation of x |
| 34 | + // it will always panic but the lint is not smart enoguh to see this (it only checks if conditions). |
27 | 35 | } else {
|
28 | 36 | x = Ok(());
|
29 |
| - x.unwrap_err(); |
| 37 | + x.unwrap_err(); // not unnecessary because of mutation of x |
| 38 | + // it will always panic but the lint is not smart enoguh to see this (it only checks if conditions). |
30 | 39 | }
|
31 | 40 | }
|
32 | 41 |
|
33 | 42 | fn test_complex_conditions() {
|
34 | 43 | let x: Result<(), ()> = Ok(());
|
35 | 44 | let y: Result<(), ()> = Ok(());
|
36 | 45 | if x.is_ok() && y.is_err() {
|
37 |
| - x.unwrap(); |
38 |
| - y.unwrap_err(); |
| 46 | + x.unwrap(); // unnecessary |
| 47 | + x.unwrap_err(); // will panic |
| 48 | + y.unwrap(); // will panic |
| 49 | + y.unwrap_err(); // unnecessary |
39 | 50 | } else {
|
40 |
| - // not clear whether unwrappable: |
| 51 | + // not statically determinable whether any of the following will always succeed or always fail: |
| 52 | + x.unwrap(); |
41 | 53 | x.unwrap_err();
|
42 | 54 | y.unwrap();
|
| 55 | + y.unwrap_err(); |
43 | 56 | }
|
44 | 57 |
|
45 | 58 | if x.is_ok() || y.is_ok() {
|
46 |
| - // not clear whether unwrappable: |
| 59 | + // not statically determinable whether any of the following will always succeed or always fail: |
47 | 60 | x.unwrap();
|
48 | 61 | y.unwrap();
|
49 | 62 | } else {
|
50 |
| - x.unwrap_err(); |
51 |
| - y.unwrap_err(); |
| 63 | + x.unwrap(); // will panic |
| 64 | + x.unwrap_err(); // unnecessary |
| 65 | + y.unwrap(); // will panic |
| 66 | + y.unwrap_err(); // unnecessary |
52 | 67 | }
|
53 | 68 | let z: Result<(), ()> = Ok(());
|
54 | 69 | if x.is_ok() && !(y.is_ok() || z.is_err()) {
|
55 |
| - x.unwrap(); |
56 |
| - y.unwrap_err(); |
57 |
| - z.unwrap(); |
| 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 |
58 | 76 | }
|
59 | 77 | if x.is_ok() || !(y.is_ok() && z.is_err()) {
|
60 |
| - // not clear what's unwrappable |
61 |
| - } else { |
62 |
| - x.unwrap_err(); |
| 78 | + // not statically determinable whether any of the following will always succeed or always fail: |
| 79 | + x.unwrap(); |
63 | 80 | y.unwrap();
|
64 |
| - z.unwrap_err(); |
| 81 | + z.unwrap(); |
| 82 | + } else { |
| 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 |
65 | 89 | }
|
66 | 90 | }
|
67 | 91 |
|
68 | 92 | fn test_nested() {
|
69 | 93 | fn nested() {
|
70 | 94 | let x = Some(());
|
71 | 95 | if x.is_some() {
|
72 |
| - x.unwrap(); |
| 96 | + x.unwrap(); // unnecessary |
| 97 | + } else { |
| 98 | + x.unwrap(); // will panic |
73 | 99 | }
|
74 | 100 | }
|
75 | 101 | }
|
0 commit comments