@@ -93,6 +93,7 @@ mod unnecessary_fold;
93
93
mod unnecessary_iter_cloned;
94
94
mod unnecessary_join;
95
95
mod unnecessary_lazy_eval;
96
+ mod unnecessary_literal_unwrap;
96
97
mod unnecessary_sort_by;
97
98
mod unnecessary_to_owned;
98
99
mod unwrap_or_else_default;
@@ -273,6 +274,56 @@ declare_clippy_lint! {
273
274
"using `.unwrap()` on `Result` or `Option`, which should at least get a better message using `expect()`"
274
275
}
275
276
277
+ declare_clippy_lint ! {
278
+ /// ### What it does
279
+ /// Checks for `.unwrap()` or `.unwrap_err()` calls on `Result`s and `.unwrap()` call on `Option`s.
280
+ ///
281
+ /// ### Why is this bad?
282
+ /// It is better to handle the `None` or `Err` case,
283
+ /// or at least call `.expect(_)` with a more helpful message. Still, for a lot of
284
+ /// quick-and-dirty code, `unwrap` is a good choice, which is why this lint is
285
+ /// `Allow` by default.
286
+ ///
287
+ /// `result.unwrap()` will let the thread panic on `Err` values.
288
+ /// Normally, you want to implement more sophisticated error handling,
289
+ /// and propagate errors upwards with `?` operator.
290
+ ///
291
+ /// Even if you want to panic on errors, not all `Error`s implement good
292
+ /// messages on display. Therefore, it may be beneficial to look at the places
293
+ /// where they may get displayed. Activate this lint to do just that.
294
+ ///
295
+ /// ### Examples
296
+ /// ```rust
297
+ /// # let option = Some(1);
298
+ /// # let result: Result<usize, ()> = Ok(1);
299
+ /// option.unwrap();
300
+ /// result.unwrap();
301
+ /// ```
302
+ ///
303
+ /// Use instead:
304
+ /// ```rust
305
+ /// # let option = Some(1);
306
+ /// # let result: Result<usize, ()> = Ok(1);
307
+ /// option.expect("more helpful message");
308
+ /// result.expect("more helpful message");
309
+ /// ```
310
+ ///
311
+ /// If [expect_used](#expect_used) is enabled, instead:
312
+ /// ```rust,ignore
313
+ /// # let option = Some(1);
314
+ /// # let result: Result<usize, ()> = Ok(1);
315
+ /// option?;
316
+ ///
317
+ /// // or
318
+ ///
319
+ /// result?;
320
+ /// ```
321
+ #[ clippy:: version = "1.69.0" ]
322
+ pub UNNECESSARY_LITERAL_UNWRAP ,
323
+ complexity,
324
+ "checks for calls of `unwrap()` or `expect()` on `Some()` that cannot fail"
325
+ }
326
+
276
327
declare_clippy_lint ! {
277
328
/// ### What it does
278
329
/// Checks for `.expect()` or `.expect_err()` calls on `Result`s and `.expect()` call on `Option`s.
@@ -3814,6 +3865,7 @@ impl Methods {
3814
3865
Some ( ( "or" , recv, [ or_arg] , or_span, _) ) => {
3815
3866
or_then_unwrap:: check ( cx, expr, recv, or_arg, or_span) ;
3816
3867
} ,
3868
+ // unnecessary_literal_unwrap::check(cx, expr, recv);
3817
3869
_ => { } ,
3818
3870
}
3819
3871
unwrap_used:: check ( cx, expr, recv, false , self . allow_unwrap_in_tests ) ;
0 commit comments