Skip to content

Commit 3f99a56

Browse files
committed
Fix panic in add_type_ascription
Assist wasn't applicable when the let statement was missing a pattern before, so we should do the same now.
1 parent df62962 commit 3f99a56

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

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

+9-8
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ pub(crate) fn add_turbo_fish(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opti
8585

8686
if let Some(let_stmt) = ctx.find_node_at_offset::<ast::LetStmt>() {
8787
if let_stmt.colon_token().is_none() {
88+
if let_stmt.pat().is_none() {
89+
return None;
90+
}
91+
8892
acc.add(
8993
AssistId("add_type_ascription", AssistKind::RefactorRewrite),
9094
"Add `: _` before assignment operator",
@@ -157,7 +161,10 @@ fn get_fish_head(number_of_arguments: usize) -> ast::GenericArgList {
157161

158162
#[cfg(test)]
159163
mod tests {
160-
use crate::tests::{check_assist, check_assist_by_label, check_assist_not_applicable};
164+
use crate::tests::{
165+
check_assist, check_assist_by_label, check_assist_not_applicable,
166+
check_assist_not_applicable_by_label,
167+
};
161168

162169
use super::*;
163170

@@ -400,19 +407,13 @@ fn main() {
400407

401408
#[test]
402409
fn add_type_ascription_missing_pattern() {
403-
check_assist_by_label(
410+
check_assist_not_applicable_by_label(
404411
add_turbo_fish,
405412
r#"
406413
fn make<T>() -> T {}
407414
fn main() {
408415
let = make$0()
409416
}
410-
"#,
411-
r#"
412-
fn make<T>() -> T {}
413-
fn main() {
414-
let : ${0:_} = make();
415-
}
416417
"#,
417418
"Add `: _` before assignment operator",
418419
);

crates/ide-assists/src/tests.rs

+5
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ pub(crate) fn check_assist_not_applicable(assist: Handler, ra_fixture: &str) {
9898
check(assist, ra_fixture, ExpectedResult::NotApplicable, None);
9999
}
100100

101+
#[track_caller]
102+
pub(crate) fn check_assist_not_applicable_by_label(assist: Handler, ra_fixture: &str, label: &str) {
103+
check(assist, ra_fixture, ExpectedResult::NotApplicable, Some(label));
104+
}
105+
101106
/// Check assist in unresolved state. Useful to check assists for lazy computation.
102107
#[track_caller]
103108
pub(crate) fn check_assist_unresolved(assist: Handler, ra_fixture: &str) {

0 commit comments

Comments
 (0)