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