Skip to content

Commit 86c2bb3

Browse files
Merge #8602
8602: Fix panic in `replace_derive_with_manual_impl` r=jonas-schievink a=jonas-schievink bors r+ Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
2 parents da6902d + c0ad9b3 commit 86c2bb3

File tree

1 file changed

+45
-28
lines changed

1 file changed

+45
-28
lines changed

crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ pub(crate) fn replace_derive_with_manual_impl(
4747
return None;
4848
}
4949

50+
if !args.syntax().text_range().contains(ctx.offset()) {
51+
cov_mark::hit!(outside_of_attr_args);
52+
return None;
53+
}
54+
5055
let trait_token = args.syntax().token_at_offset(ctx.offset()).find(|t| t.kind() == IDENT)?;
5156
let trait_name = trait_token.text();
5257

@@ -207,7 +212,7 @@ mod tests {
207212
fn add_custom_impl_debug() {
208213
check_assist(
209214
replace_derive_with_manual_impl,
210-
"
215+
r#"
211216
mod fmt {
212217
pub struct Error;
213218
pub type Result = Result<(), Error>;
@@ -221,8 +226,8 @@ mod fmt {
221226
struct Foo {
222227
bar: String,
223228
}
224-
",
225-
"
229+
"#,
230+
r#"
226231
mod fmt {
227232
pub struct Error;
228233
pub type Result = Result<(), Error>;
@@ -241,14 +246,14 @@ impl fmt::Debug for Foo {
241246
${0:todo!()}
242247
}
243248
}
244-
",
249+
"#,
245250
)
246251
}
247252
#[test]
248253
fn add_custom_impl_all() {
249254
check_assist(
250255
replace_derive_with_manual_impl,
251-
"
256+
r#"
252257
mod foo {
253258
pub trait Bar {
254259
type Qux;
@@ -263,8 +268,8 @@ mod foo {
263268
struct Foo {
264269
bar: String,
265270
}
266-
",
267-
"
271+
"#,
272+
r#"
268273
mod foo {
269274
pub trait Bar {
270275
type Qux;
@@ -290,110 +295,122 @@ impl foo::Bar for Foo {
290295
todo!()
291296
}
292297
}
293-
",
298+
"#,
294299
)
295300
}
296301
#[test]
297302
fn add_custom_impl_for_unique_input() {
298303
check_assist(
299304
replace_derive_with_manual_impl,
300-
"
305+
r#"
301306
#[derive(Debu$0g)]
302307
struct Foo {
303308
bar: String,
304309
}
305-
",
306-
"
310+
"#,
311+
r#"
307312
struct Foo {
308313
bar: String,
309314
}
310315
311316
impl Debug for Foo {
312317
$0
313318
}
314-
",
319+
"#,
315320
)
316321
}
317322

318323
#[test]
319324
fn add_custom_impl_for_with_visibility_modifier() {
320325
check_assist(
321326
replace_derive_with_manual_impl,
322-
"
327+
r#"
323328
#[derive(Debug$0)]
324329
pub struct Foo {
325330
bar: String,
326331
}
327-
",
328-
"
332+
"#,
333+
r#"
329334
pub struct Foo {
330335
bar: String,
331336
}
332337
333338
impl Debug for Foo {
334339
$0
335340
}
336-
",
341+
"#,
337342
)
338343
}
339344

340345
#[test]
341346
fn add_custom_impl_when_multiple_inputs() {
342347
check_assist(
343348
replace_derive_with_manual_impl,
344-
"
349+
r#"
345350
#[derive(Display, Debug$0, Serialize)]
346351
struct Foo {}
347-
",
348-
"
352+
"#,
353+
r#"
349354
#[derive(Display, Serialize)]
350355
struct Foo {}
351356
352357
impl Debug for Foo {
353358
$0
354359
}
355-
",
360+
"#,
356361
)
357362
}
358363

359364
#[test]
360365
fn test_ignore_derive_macro_without_input() {
361366
check_assist_not_applicable(
362367
replace_derive_with_manual_impl,
363-
"
368+
r#"
364369
#[derive($0)]
365370
struct Foo {}
366-
",
371+
"#,
367372
)
368373
}
369374

370375
#[test]
371376
fn test_ignore_if_cursor_on_param() {
372377
check_assist_not_applicable(
373378
replace_derive_with_manual_impl,
374-
"
379+
r#"
375380
#[derive$0(Debug)]
376381
struct Foo {}
377-
",
382+
"#,
378383
);
379384

380385
check_assist_not_applicable(
381386
replace_derive_with_manual_impl,
382-
"
387+
r#"
383388
#[derive(Debug)$0]
384389
struct Foo {}
385-
",
390+
"#,
386391
)
387392
}
388393

389394
#[test]
390395
fn test_ignore_if_not_derive() {
391396
check_assist_not_applicable(
392397
replace_derive_with_manual_impl,
393-
"
398+
r#"
394399
#[allow(non_camel_$0case_types)]
395400
struct Foo {}
396-
",
401+
"#,
397402
)
398403
}
404+
405+
#[test]
406+
fn works_at_start_of_file() {
407+
cov_mark::check!(outside_of_attr_args);
408+
check_assist_not_applicable(
409+
replace_derive_with_manual_impl,
410+
r#"
411+
$0#[derive(Debug)]
412+
struct S;
413+
"#,
414+
);
415+
}
399416
}

0 commit comments

Comments
 (0)