Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion crates/ide-assists/src/handlers/replace_let_with_if_let.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ use crate::{AssistContext, AssistId, Assists};
pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
let let_kw = ctx.find_token_syntax_at_offset(T![let])?;
let let_stmt = let_kw.parent().and_then(ast::LetStmt::cast)?;
// Bail on malformed input (e.g. unterminated string literal in the initializer
// swallows the trailing `;` and subsequent tokens). `make::expr_if` would
// otherwise re-parse a broken format string and panic.
let_stmt.semicolon_token()?;
let init = let_stmt.initializer()?;
let original_pat = let_stmt.pat()?;

Expand Down Expand Up @@ -101,7 +105,7 @@ fn let_expr_needs_paren(expr: &ast::Expr) -> bool {

#[cfg(test)]
mod tests {
use crate::tests::check_assist;
use crate::tests::{check_assist, check_assist_not_applicable};

use super::*;

Expand Down Expand Up @@ -224,4 +228,16 @@ fn main() {
",
)
}

#[test]
fn replace_let_not_applicable_on_unterminated_string() {
check_assist_not_applicable(
replace_let_with_if_let,
r#"
fn main() {
$0let s = "foo
}
"#,
)
}
}