@@ -73,7 +73,7 @@ declare_clippy_lint! {
73
73
/// **Known problems:** None.
74
74
///
75
75
/// **Example:**
76
- /// Using unwrap on an `Option `:
76
+ /// Using unwrap on an `Result `:
77
77
///
78
78
/// ```rust
79
79
/// let res: Result<usize, ()> = Ok(1);
@@ -91,6 +91,63 @@ declare_clippy_lint! {
91
91
"using `Result.unwrap()`, which might be better handled"
92
92
}
93
93
94
+ declare_clippy_lint ! {
95
+ /// **What it does:** Checks for `.expect()` calls on `Option`s.
96
+ ///
97
+ /// **Why is this bad?** Usually it is better to handle the `None` case. Still,
98
+ /// for a lot of quick-and-dirty code, `expect` is a good choice, which is why
99
+ /// this lint is `Allow` by default.
100
+ ///
101
+ /// **Known problems:** None.
102
+ ///
103
+ /// **Example:**
104
+ ///
105
+ /// Using expect on an `Option`:
106
+ ///
107
+ /// ```rust
108
+ /// let opt = Some(1);
109
+ /// opt.expect("one");
110
+ /// ```
111
+ ///
112
+ /// Better:
113
+ ///
114
+ /// ```rust
115
+ /// let opt = Some(1);
116
+ /// opt?;
117
+ /// ```
118
+ pub OPTION_EXPECT_USED ,
119
+ restriction,
120
+ "using `Option.expect()`, which might be better handled"
121
+ }
122
+
123
+ declare_clippy_lint ! {
124
+ /// **What it does:** Checks for `.expect()` calls on `Result`s.
125
+ ///
126
+ /// **Why is this bad?** `result.expect()` will let the thread panic on `Err`
127
+ /// values. Normally, you want to implement more sophisticated error handling,
128
+ /// and propagate errors upwards with `try!`.
129
+ ///
130
+ /// **Known problems:** None.
131
+ ///
132
+ /// **Example:**
133
+ /// Using expect on an `Result`:
134
+ ///
135
+ /// ```rust
136
+ /// let res: Result<usize, ()> = Ok(1);
137
+ /// res.expect("one");
138
+ /// ```
139
+ ///
140
+ /// Better:
141
+ ///
142
+ /// ```rust
143
+ /// let res: Result<usize, ()> = Ok(1);
144
+ /// res?;
145
+ /// ```
146
+ pub RESULT_EXPECT_USED ,
147
+ restriction,
148
+ "using `Result.expect()`, which might be better handled"
149
+ }
150
+
94
151
declare_clippy_lint ! {
95
152
/// **What it does:** Checks for methods that should live in a trait
96
153
/// implementation of a `std` trait (see [llogiq's blog
@@ -1037,6 +1094,8 @@ declare_clippy_lint! {
1037
1094
declare_lint_pass ! ( Methods => [
1038
1095
OPTION_UNWRAP_USED ,
1039
1096
RESULT_UNWRAP_USED ,
1097
+ OPTION_EXPECT_USED ,
1098
+ RESULT_EXPECT_USED ,
1040
1099
SHOULD_IMPLEMENT_TRAIT ,
1041
1100
WRONG_SELF_CONVENTION ,
1042
1101
WRONG_PUB_SELF_CONVENTION ,
@@ -1095,6 +1154,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
1095
1154
[ "unwrap" , "get_mut" ] => lint_get_unwrap ( cx, expr, arg_lists[ 1 ] , true ) ,
1096
1155
[ "unwrap" , ..] => lint_unwrap ( cx, expr, arg_lists[ 0 ] ) ,
1097
1156
[ "expect" , "ok" ] => lint_ok_expect ( cx, expr, arg_lists[ 1 ] ) ,
1157
+ [ "expect" , ..] => lint_expect ( cx, expr, arg_lists[ 0 ] ) ,
1098
1158
[ "unwrap_or" , "map" ] => option_map_unwrap_or:: lint ( cx, expr, arg_lists[ 1 ] , arg_lists[ 0 ] ) ,
1099
1159
[ "unwrap_or_else" , "map" ] => lint_map_unwrap_or_else ( cx, expr, arg_lists[ 1 ] , arg_lists[ 0 ] ) ,
1100
1160
[ "map_or" , ..] => lint_map_or_none ( cx, expr, arg_lists[ 0 ] ) ,
@@ -2063,6 +2123,31 @@ fn lint_unwrap(cx: &LateContext<'_, '_>, expr: &hir::Expr, unwrap_args: &[hir::E
2063
2123
}
2064
2124
}
2065
2125
2126
+ /// lint use of `expect()` for `Option`s and `Result`s
2127
+ fn lint_expect ( cx : & LateContext < ' _ , ' _ > , expr : & hir:: Expr , expect_args : & [ hir:: Expr ] ) {
2128
+ let obj_ty = walk_ptrs_ty ( cx. tables . expr_ty ( & expect_args[ 0 ] ) ) ;
2129
+
2130
+ let mess = if match_type ( cx, obj_ty, & paths:: OPTION ) {
2131
+ Some ( ( OPTION_EXPECT_USED , "an Option" , "None" ) )
2132
+ } else if match_type ( cx, obj_ty, & paths:: RESULT ) {
2133
+ Some ( ( RESULT_EXPECT_USED , "a Result" , "Err" ) )
2134
+ } else {
2135
+ None
2136
+ } ;
2137
+
2138
+ if let Some ( ( lint, kind, none_value) ) = mess {
2139
+ span_lint (
2140
+ cx,
2141
+ lint,
2142
+ expr. span ,
2143
+ & format ! (
2144
+ "used expect() on {} value. If this value is an {} it will panic" ,
2145
+ kind, none_value
2146
+ ) ,
2147
+ ) ;
2148
+ }
2149
+ }
2150
+
2066
2151
/// lint use of `ok().expect()` for `Result`s
2067
2152
fn lint_ok_expect ( cx : & LateContext < ' _ , ' _ > , expr : & hir:: Expr , ok_args : & [ hir:: Expr ] ) {
2068
2153
if_chain ! {
0 commit comments