@@ -151,20 +151,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
151
151
debug ! ( ">> type-checking: expr={:?} expected={:?}" ,
152
152
expr, expected) ;
153
153
154
- // If when desugaring the try block we ok-wrapped an expression that diverges
155
- // (e.g. `try { return }`) then technically the ok-wrapping expression is unreachable.
156
- // But since it is autogenerated code the resulting warning is confusing for the user
157
- // so we want avoid generating it.
158
- // Ditto for the autogenerated `Try::from_ok(())` at the end of e.g. `try { return; }`.
159
- let ( is_try_block_ok_wrapped_expr, is_try_block_generated_expr) = match expr. node {
160
- ExprKind :: Call ( _, ref args) if expr. span . is_desugaring ( DesugaringKind :: TryBlock ) => {
161
- ( true , args. len ( ) == 1 && args[ 0 ] . span . is_desugaring ( DesugaringKind :: TryBlock ) )
162
- }
163
- _ => ( false , false ) ,
154
+ // True if `expr` is a `Try::from_ok(())` that is a result of desugaring a try block
155
+ // without the final expr (e.g. `try { return; }`). We don't want to generate an
156
+ // unreachable_code lint for it since warnings for autogenerated code are confusing.
157
+ let is_try_block_generated_unit_expr = match expr. node {
158
+ ExprKind :: Call ( _, ref args) if expr. span . is_desugaring ( DesugaringKind :: TryBlock ) =>
159
+ args. len ( ) == 1 && args[ 0 ] . span . is_desugaring ( DesugaringKind :: TryBlock ) ,
160
+
161
+ _ => false ,
164
162
} ;
165
163
166
164
// Warn for expressions after diverging siblings.
167
- if !is_try_block_generated_expr {
165
+ if !is_try_block_generated_unit_expr {
168
166
self . warn_if_unreachable ( expr. hir_id , expr. span , "expression" ) ;
169
167
}
170
168
@@ -177,15 +175,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
177
175
let ty = self . check_expr_kind ( expr, expected, needs) ;
178
176
179
177
// Warn for non-block expressions with diverging children.
180
- if !is_try_block_ok_wrapped_expr {
181
- match expr. kind {
182
- ExprKind :: Block ( ..) | ExprKind :: Loop ( ..) | ExprKind :: Match ( ..) => { } ,
183
- ExprKind :: Call ( ref callee, _) =>
184
- self . warn_if_unreachable ( expr. hir_id , callee. span , "call" ) ,
185
- ExprKind :: MethodCall ( _, ref span, _) =>
186
- self . warn_if_unreachable ( expr. hir_id , * span, "call" ) ,
187
- _ => self . warn_if_unreachable ( expr. hir_id , expr. span , "expression" ) ,
188
- }
178
+ match expr. kind {
179
+ ExprKind :: Block ( ..) | ExprKind :: Loop ( ..) | ExprKind :: Match ( ..) => { } ,
180
+ // If `expr` is a result of desugaring the try block and is an ok-wrapped
181
+ // diverging expression (e.g. it arose from desugaring of `try { return }`),
182
+ // we skip issuing a warning because it is autogenerated code.
183
+ ExprKind :: Call ( ..) if expr. span . is_desugaring ( DesugaringKind :: TryBlock ) => { } ,
184
+ ExprKind :: Call ( ref callee, _) =>
185
+ self . warn_if_unreachable ( expr. hir_id , callee. span , "call" ) ,
186
+ ExprKind :: MethodCall ( _, ref span, _) =>
187
+ self . warn_if_unreachable ( expr. hir_id , * span, "call" ) ,
188
+ _ => self . warn_if_unreachable ( expr. hir_id , expr. span , "expression" ) ,
189
189
}
190
190
191
191
// Any expression that produces a value of type `!` must have diverged
0 commit comments