Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 900efbe

Browse files
committed
Auto merge of rust-lang#14260 - Veykril:if-let-match-adjust, r=Veykril
fix: Adjust `replace_match_with_if_let` applicability range It currently trigger in the entirety of match which is less than ideal
2 parents 0a956ec + 7be48ac commit 900efbe

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

crates/ide-assists/src/handlers/replace_if_let_with_match.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,11 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext<'
102102
return None;
103103
}
104104

105+
let let_ = if pat_seen { " let" } else { "" };
106+
105107
acc.add(
106108
AssistId("replace_if_let_with_match", AssistKind::RefactorRewrite),
107-
"Replace if let with match",
109+
format!("Replace if{let_} with match"),
108110
available_range,
109111
move |edit| {
110112
let match_expr = {
@@ -210,8 +212,17 @@ fn make_else_arm(
210212
// ```
211213
pub(crate) fn replace_match_with_if_let(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
212214
let match_expr: ast::MatchExpr = ctx.find_node_at_offset()?;
215+
let match_arm_list = match_expr.match_arm_list()?;
216+
let available_range = TextRange::new(
217+
match_expr.syntax().text_range().start(),
218+
match_arm_list.syntax().text_range().start(),
219+
);
220+
let cursor_in_range = available_range.contains_range(ctx.selection_trimmed());
221+
if !cursor_in_range {
222+
return None;
223+
}
213224

214-
let mut arms = match_expr.match_arm_list()?.arms();
225+
let mut arms = match_arm_list.arms();
215226
let (first_arm, second_arm) = (arms.next()?, arms.next()?);
216227
if arms.next().is_some() || first_arm.guard().is_some() || second_arm.guard().is_some() {
217228
return None;
@@ -226,10 +237,20 @@ pub(crate) fn replace_match_with_if_let(acc: &mut Assists, ctx: &AssistContext<'
226237
)?;
227238
let scrutinee = match_expr.expr()?;
228239

240+
let let_ = match &if_let_pat {
241+
ast::Pat::LiteralPat(p)
242+
if p.literal()
243+
.map(|it| it.token().kind())
244+
.map_or(false, |it| it == T![true] || it == T![false]) =>
245+
{
246+
""
247+
}
248+
_ => " let",
249+
};
229250
let target = match_expr.syntax().text_range();
230251
acc.add(
231252
AssistId("replace_match_with_if_let", AssistKind::RefactorRewrite),
232-
"Replace match with if let",
253+
format!("Replace match with if{let_}"),
233254
target,
234255
move |edit| {
235256
fn make_block_expr(expr: ast::Expr) -> ast::BlockExpr {

0 commit comments

Comments
 (0)