From 66241589690bff9488b64101b281a7a2b3a2393f Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 20 Apr 2021 16:25:24 +0200 Subject: [PATCH] Fix panic in `replace_derive_with_manual_impl` --- .../handlers/replace_derive_with_manual_impl.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs b/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs index 870a8d4ffba..47d8b9c9161 100644 --- a/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs +++ b/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs @@ -47,6 +47,11 @@ pub(crate) fn replace_derive_with_manual_impl( return None; } + if !args.syntax().text_range().contains(ctx.offset()) { + cov_mark::hit!(outside_of_attr_args); + return None; + } + let trait_token = args.syntax().token_at_offset(ctx.offset()).find(|t| t.kind() == IDENT)?; let trait_name = trait_token.text(); @@ -396,4 +401,16 @@ struct Foo {} ", ) } + + #[test] + fn works_at_start_of_file() { + cov_mark::check!(outside_of_attr_args); + check_assist_not_applicable( + replace_derive_with_manual_impl, + r#" +$0#[derive(Debug)] +struct S; + "#, + ); + } }