]>
Commit | Line | Data |
---|---|---|
fe692bf9 FG |
1 | #![warn(clippy::unnecessary_literal_unwrap)] |
2 | #![allow(unreachable_code)] | |
3 | #![allow(clippy::unnecessary_lazy_evaluations, clippy::let_unit_value)] | |
781aab86 | 4 | //@no-rustfix |
fe692bf9 FG |
5 | fn unwrap_option_some() { |
6 | let val = Some(1); | |
7 | let _val2 = val.unwrap(); | |
781aab86 | 8 | //~^ ERROR: used `unwrap()` on `Some` value |
fe692bf9 | 9 | let _val2 = val.expect("this never happens"); |
781aab86 | 10 | //~^ ERROR: used `expect()` on `Some` value |
fe692bf9 FG |
11 | } |
12 | ||
13 | fn unwrap_option_some_context() { | |
14 | let _val = Some::<usize>([1, 2, 3].iter().sum()).unwrap(); | |
781aab86 | 15 | //~^ ERROR: used `unwrap()` on `Some` value |
fe692bf9 | 16 | let _val = Some::<usize>([1, 2, 3].iter().sum()).expect("this never happens"); |
781aab86 | 17 | //~^ ERROR: used `expect()` on `Some` value |
fe692bf9 FG |
18 | |
19 | let val = Some::<usize>([1, 2, 3].iter().sum()); | |
20 | let _val2 = val.unwrap(); | |
781aab86 | 21 | //~^ ERROR: used `unwrap()` on `Some` value |
fe692bf9 | 22 | let _val2 = val.expect("this never happens"); |
781aab86 | 23 | //~^ ERROR: used `expect()` on `Some` value |
fe692bf9 FG |
24 | } |
25 | ||
26 | fn unwrap_option_none() { | |
27 | let val = None::<()>; | |
28 | let _val2 = val.unwrap(); | |
781aab86 | 29 | //~^ ERROR: used `unwrap()` on `None` value |
fe692bf9 | 30 | let _val2 = val.expect("this always happens"); |
781aab86 | 31 | //~^ ERROR: used `expect()` on `None` value |
fe692bf9 | 32 | let _val3: u8 = None.unwrap_or_default(); |
781aab86 | 33 | //~^ ERROR: used `unwrap_or_default()` on `None` value |
fe692bf9 | 34 | None::<()>.unwrap_or_default(); |
781aab86 | 35 | //~^ ERROR: used `unwrap_or_default()` on `None` value |
fe692bf9 FG |
36 | } |
37 | ||
38 | fn unwrap_result_ok() { | |
39 | let val = Ok::<_, ()>(1); | |
40 | let _val2 = val.unwrap(); | |
781aab86 | 41 | //~^ ERROR: used `unwrap()` on `Ok` value |
fe692bf9 | 42 | let _val2 = val.expect("this never happens"); |
781aab86 | 43 | //~^ ERROR: used `expect()` on `Ok` value |
fe692bf9 | 44 | let _val2 = val.unwrap_err(); |
781aab86 | 45 | //~^ ERROR: used `unwrap_err()` on `Ok` value |
fe692bf9 | 46 | let _val2 = val.expect_err("this always happens"); |
781aab86 | 47 | //~^ ERROR: used `expect_err()` on `Ok` value |
fe692bf9 FG |
48 | } |
49 | ||
50 | fn unwrap_result_ok_context() { | |
51 | let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).unwrap(); | |
781aab86 | 52 | //~^ ERROR: used `unwrap()` on `Ok` value |
fe692bf9 | 53 | let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).expect("this never happens"); |
781aab86 | 54 | //~^ ERROR: used `expect()` on `Ok` value |
fe692bf9 | 55 | let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).unwrap_err(); |
781aab86 | 56 | //~^ ERROR: used `unwrap_err()` on `Ok` value |
fe692bf9 | 57 | let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).expect_err("this always happens"); |
781aab86 | 58 | //~^ ERROR: used `expect_err()` on `Ok` value |
fe692bf9 FG |
59 | |
60 | let val = Ok::<usize, ()>([1, 2, 3].iter().sum()); | |
61 | let _val2 = val.unwrap(); | |
781aab86 | 62 | //~^ ERROR: used `unwrap()` on `Ok` value |
fe692bf9 | 63 | let _val2 = val.expect("this never happens"); |
781aab86 | 64 | //~^ ERROR: used `expect()` on `Ok` value |
fe692bf9 | 65 | let _val2 = val.unwrap_err(); |
781aab86 | 66 | //~^ ERROR: used `unwrap_err()` on `Ok` value |
fe692bf9 | 67 | let _val2 = val.expect_err("this always happens"); |
781aab86 | 68 | //~^ ERROR: used `expect_err()` on `Ok` value |
fe692bf9 FG |
69 | } |
70 | ||
71 | fn unwrap_result_err() { | |
72 | let val = Err::<(), _>(1); | |
73 | let _val2 = val.unwrap_err(); | |
781aab86 | 74 | //~^ ERROR: used `unwrap_err()` on `Err` value |
fe692bf9 | 75 | let _val2 = val.expect_err("this never happens"); |
781aab86 | 76 | //~^ ERROR: used `expect_err()` on `Err` value |
fe692bf9 | 77 | let _val2 = val.unwrap(); |
781aab86 | 78 | //~^ ERROR: used `unwrap()` on `Err` value |
fe692bf9 | 79 | let _val2 = val.expect("this always happens"); |
781aab86 | 80 | //~^ ERROR: used `expect()` on `Err` value |
fe692bf9 FG |
81 | } |
82 | ||
83 | fn unwrap_result_err_context() { | |
84 | let _val = Err::<(), usize>([1, 2, 3].iter().sum()).unwrap_err(); | |
781aab86 | 85 | //~^ ERROR: used `unwrap_err()` on `Err` value |
fe692bf9 | 86 | let _val = Err::<(), usize>([1, 2, 3].iter().sum()).expect_err("this never happens"); |
781aab86 | 87 | //~^ ERROR: used `expect_err()` on `Err` value |
fe692bf9 | 88 | let _val = Err::<(), usize>([1, 2, 3].iter().sum()).unwrap(); |
781aab86 | 89 | //~^ ERROR: used `unwrap()` on `Err` value |
fe692bf9 | 90 | let _val = Err::<(), usize>([1, 2, 3].iter().sum()).expect("this always happens"); |
781aab86 | 91 | //~^ ERROR: used `expect()` on `Err` value |
fe692bf9 FG |
92 | |
93 | let val = Err::<(), usize>([1, 2, 3].iter().sum()); | |
94 | let _val2 = val.unwrap_err(); | |
781aab86 | 95 | //~^ ERROR: used `unwrap_err()` on `Err` value |
fe692bf9 | 96 | let _val2 = val.expect_err("this never happens"); |
781aab86 | 97 | //~^ ERROR: used `expect_err()` on `Err` value |
fe692bf9 | 98 | let _val2 = val.unwrap(); |
781aab86 | 99 | //~^ ERROR: used `unwrap()` on `Err` value |
fe692bf9 | 100 | let _val2 = val.expect("this always happens"); |
781aab86 | 101 | //~^ ERROR: used `expect()` on `Err` value |
fe692bf9 FG |
102 | } |
103 | ||
104 | fn unwrap_methods_option() { | |
105 | let val = Some(1); | |
106 | let _val2 = val.unwrap_or(2); | |
781aab86 | 107 | //~^ ERROR: used `unwrap_or()` on `Some` value |
fe692bf9 | 108 | let _val2 = val.unwrap_or_default(); |
781aab86 | 109 | //~^ ERROR: used `unwrap_or_default()` on `Some` value |
fe692bf9 | 110 | let _val2 = val.unwrap_or_else(|| 2); |
781aab86 | 111 | //~^ ERROR: used `unwrap_or_else()` on `Some` value |
fe692bf9 FG |
112 | } |
113 | ||
114 | fn unwrap_methods_option_context() { | |
115 | let _val = Some::<usize>([1, 2, 3].iter().sum()).unwrap_or(2); | |
781aab86 | 116 | //~^ ERROR: used `unwrap_or()` on `Some` value |
fe692bf9 | 117 | let _val = Some::<usize>([1, 2, 3].iter().sum()).unwrap_or_default(); |
781aab86 | 118 | //~^ ERROR: used `unwrap_or_default()` on `Some` value |
fe692bf9 | 119 | let _val = Some::<usize>([1, 2, 3].iter().sum()).unwrap_or_else(|| 2); |
781aab86 | 120 | //~^ ERROR: used `unwrap_or_else()` on `Some` value |
fe692bf9 FG |
121 | |
122 | let val = Some::<usize>([1, 2, 3].iter().sum()); | |
123 | let _val2 = val.unwrap_or(2); | |
781aab86 | 124 | //~^ ERROR: used `unwrap_or()` on `Some` value |
fe692bf9 | 125 | let _val2 = val.unwrap_or_default(); |
781aab86 | 126 | //~^ ERROR: used `unwrap_or_default()` on `Some` value |
fe692bf9 | 127 | let _val2 = val.unwrap_or_else(|| 2); |
781aab86 | 128 | //~^ ERROR: used `unwrap_or_else()` on `Some` value |
fe692bf9 FG |
129 | } |
130 | ||
131 | fn unwrap_methods_result() { | |
132 | let val = Ok::<_, ()>(1); | |
133 | let _val2 = val.unwrap_or(2); | |
781aab86 | 134 | //~^ ERROR: used `unwrap_or()` on `Ok` value |
fe692bf9 | 135 | let _val2 = val.unwrap_or_default(); |
781aab86 | 136 | //~^ ERROR: used `unwrap_or_default()` on `Ok` value |
fe692bf9 | 137 | let _val2 = val.unwrap_or_else(|_| 2); |
781aab86 | 138 | //~^ ERROR: used `unwrap_or_else()` on `Ok` value |
fe692bf9 FG |
139 | } |
140 | ||
141 | fn unwrap_methods_result_context() { | |
142 | let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).unwrap_or(2); | |
781aab86 | 143 | //~^ ERROR: used `unwrap_or()` on `Ok` value |
fe692bf9 | 144 | let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).unwrap_or_default(); |
781aab86 | 145 | //~^ ERROR: used `unwrap_or_default()` on `Ok` value |
fe692bf9 | 146 | let _val = Ok::<usize, ()>([1, 2, 3].iter().sum()).unwrap_or_else(|_| 2); |
781aab86 | 147 | //~^ ERROR: used `unwrap_or_else()` on `Ok` value |
fe692bf9 FG |
148 | |
149 | let val = Ok::<usize, ()>([1, 2, 3].iter().sum()); | |
150 | let _val2 = val.unwrap_or(2); | |
781aab86 | 151 | //~^ ERROR: used `unwrap_or()` on `Ok` value |
fe692bf9 | 152 | let _val2 = val.unwrap_or_default(); |
781aab86 | 153 | //~^ ERROR: used `unwrap_or_default()` on `Ok` value |
fe692bf9 | 154 | let _val2 = val.unwrap_or_else(|_| 2); |
781aab86 | 155 | //~^ ERROR: used `unwrap_or_else()` on `Ok` value |
fe692bf9 FG |
156 | } |
157 | ||
158 | fn main() { | |
159 | unwrap_option_some(); | |
160 | unwrap_option_some_context(); | |
161 | unwrap_option_none(); | |
162 | unwrap_result_ok(); | |
163 | unwrap_result_ok_context(); | |
164 | unwrap_result_err(); | |
165 | unwrap_result_err_context(); | |
166 | unwrap_methods_option(); | |
167 | unwrap_methods_option_context(); | |
168 | unwrap_methods_result(); | |
169 | unwrap_methods_result_context(); | |
170 | } |