From c6305c56592b49ae817e550e3cd42ef704e2307d Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 6 Feb 2023 12:38:57 +0100 Subject: [PATCH] fix: Don't panic on broken syntax trees in adjustment inlay hints --- crates/ide/src/inlay_hints/adjustment.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/crates/ide/src/inlay_hints/adjustment.rs b/crates/ide/src/inlay_hints/adjustment.rs index 45e85a338a4..188eb7f977b 100644 --- a/crates/ide/src/inlay_hints/adjustment.rs +++ b/crates/ide/src/inlay_hints/adjustment.rs @@ -6,6 +6,7 @@ use hir::{Adjust, Adjustment, AutoBorrow, HirDisplay, Mutability, PointerCast, Safety, Semantics}; use ide_db::RootDatabase; +use stdx::never; use syntax::{ ast::{self, make, AstNode}, ted, @@ -210,16 +211,21 @@ fn needs_parens_for_adjustment_hints(expr: &ast::Expr, postfix: bool) -> (bool, ted::replace(expr.syntax(), dummy_expr.syntax()); let parent = dummy_expr.syntax().parent(); - let expr = if postfix { - let ast::Expr::TryExpr(e) = &dummy_expr else { unreachable!() }; - let Some(ast::Expr::ParenExpr(e)) = e.expr() else { unreachable!() }; + let Some(expr) = (|| { + if postfix { + let ast::Expr::TryExpr(e) = &dummy_expr else { return None }; + let Some(ast::Expr::ParenExpr(e)) = e.expr() else { return None }; - e.expr().unwrap() - } else { - let ast::Expr::RefExpr(e) = &dummy_expr else { unreachable!() }; - let Some(ast::Expr::ParenExpr(e)) = e.expr() else { unreachable!() }; + e.expr() + } else { + let ast::Expr::RefExpr(e) = &dummy_expr else { return None }; + let Some(ast::Expr::ParenExpr(e)) = e.expr() else { return None }; - e.expr().unwrap() + e.expr() + } + })() else { + never!("broken syntax tree?\n{:?}\n{:?}", expr, dummy_expr); + return (true, true) }; // At this point