@@ -33,40 +33,15 @@ use crate::utils::{
3333} ;
3434
3535declare_clippy_lint ! {
36- /// **What it does:** Checks for `.unwrap()` calls on `Option`s.
36+ /// **What it does:** Checks for `.unwrap()` calls on `Option`s and on `Result`s .
3737 ///
38- /// **Why is this bad?** Usually it is better to handle the `None` case, or to
39- /// at least call `.expect(_)` with a more helpful message. Still, for a lot of
38+ /// **Why is this bad?** It is better to handle the `None` or `Err` case,
39+ /// or at least call `.expect(_)` with a more helpful message. Still, for a lot of
4040 /// quick-and-dirty code, `unwrap` is a good choice, which is why this lint is
4141 /// `Allow` by default.
4242 ///
43- /// **Known problems:** None.
44- ///
45- /// **Example:**
46- ///
47- /// Using unwrap on an `Option`:
48- ///
49- /// ```rust
50- /// let opt = Some(1);
51- /// opt.unwrap();
52- /// ```
53- ///
54- /// Better:
55- ///
56- /// ```rust
57- /// let opt = Some(1);
58- /// opt.expect("more helpful message");
59- /// ```
60- pub OPTION_UNWRAP_USED ,
61- restriction,
62- "using `Option.unwrap()`, which should at least get a better message using `expect()`"
63- }
64-
65- declare_clippy_lint ! {
66- /// **What it does:** Checks for `.unwrap()` calls on `Result`s.
67- ///
68- /// **Why is this bad?** `result.unwrap()` will let the thread panic on `Err`
69- /// values. Normally, you want to implement more sophisticated error handling,
43+ /// `result.unwrap()` will let the thread panic on `Err` values.
44+ /// Normally, you want to implement more sophisticated error handling,
7045 /// and propagate errors upwards with `?` operator.
7146 ///
7247 /// Even if you want to panic on errors, not all `Error`s implement good
@@ -75,23 +50,31 @@ declare_clippy_lint! {
7550 ///
7651 /// **Known problems:** None.
7752 ///
78- /// **Example:**
79- /// Using unwrap on an `Result`:
80- ///
53+ /// **Examples:**
8154 /// ```rust
82- /// let res: Result<usize, ()> = Ok(1);
83- /// res.unwrap();
55+ /// # let opt = Some(1);
56+ ///
57+ /// // Bad
58+ /// opt.unwrap();
59+ ///
60+ /// // Good
61+ /// opt.expect("more helpful message");
8462 /// ```
8563 ///
86- /// Better:
64+ /// // or
8765 ///
8866 /// ```rust
89- /// let res: Result<usize, ()> = Ok(1);
67+ /// # let res: Result<usize, ()> = Ok(1);
68+ ///
69+ /// // Bad
70+ /// res.unwrap();
71+ ///
72+ /// // Good
9073 /// res.expect("more helpful message");
9174 /// ```
92- pub RESULT_UNWRAP_USED ,
75+ pub UNWRAP_USED ,
9376 restriction,
94- "using `Result .unwrap()`, which might be better handled "
77+ "using `.unwrap()` on `Result` or `Option` , which should at least get a better message using `expect()` "
9578}
9679
9780declare_clippy_lint ! {
@@ -1267,8 +1250,7 @@ declare_clippy_lint! {
12671250}
12681251
12691252declare_lint_pass ! ( Methods => [
1270- OPTION_UNWRAP_USED ,
1271- RESULT_UNWRAP_USED ,
1253+ UNWRAP_USED ,
12721254 OPTION_EXPECT_USED ,
12731255 RESULT_EXPECT_USED ,
12741256 SHOULD_IMPLEMENT_TRAIT ,
@@ -2397,9 +2379,9 @@ fn lint_unwrap(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, unwrap_args: &[hi
23972379 let obj_ty = walk_ptrs_ty ( cx. tables . expr_ty ( & unwrap_args[ 0 ] ) ) ;
23982380
23992381 let mess = if is_type_diagnostic_item ( cx, obj_ty, sym ! ( option_type) ) {
2400- Some ( ( OPTION_UNWRAP_USED , "an Option" , "None" ) )
2382+ Some ( ( UNWRAP_USED , "an Option" , "None" ) )
24012383 } else if is_type_diagnostic_item ( cx, obj_ty, sym ! ( result_type) ) {
2402- Some ( ( RESULT_UNWRAP_USED , "a Result" , "Err" ) )
2384+ Some ( ( UNWRAP_USED , "a Result" , "Err" ) )
24032385 } else {
24042386 None
24052387 } ;
0 commit comments