From 66241589690bff9488b64101b281a7a2b3a2393f Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 20 Apr 2021 16:25:24 +0200 Subject: Fix panic in `replace_derive_with_manual_impl` --- .../src/handlers/replace_derive_with_manual_impl.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs') 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 870a8d4ff..47d8b9c91 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; + "#, + ); + } } -- cgit v1.2.3 From c0ad9b35814b6eb29d7cb4f3c38b84d21db4dec8 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 20 Apr 2021 16:26:07 +0200 Subject: Follow testing style guide --- .../handlers/replace_derive_with_manual_impl.rs | 56 +++++++++++----------- 1 file changed, 28 insertions(+), 28 deletions(-) (limited to 'crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs') 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 47d8b9c91..694d897d1 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 @@ -212,7 +212,7 @@ mod tests { fn add_custom_impl_debug() { check_assist( replace_derive_with_manual_impl, - " + r#" mod fmt { pub struct Error; pub type Result = Result<(), Error>; @@ -226,8 +226,8 @@ mod fmt { struct Foo { bar: String, } -", - " +"#, + r#" mod fmt { pub struct Error; pub type Result = Result<(), Error>; @@ -246,14 +246,14 @@ impl fmt::Debug for Foo { ${0:todo!()} } } -", +"#, ) } #[test] fn add_custom_impl_all() { check_assist( replace_derive_with_manual_impl, - " + r#" mod foo { pub trait Bar { type Qux; @@ -268,8 +268,8 @@ mod foo { struct Foo { bar: String, } -", - " +"#, + r#" mod foo { pub trait Bar { type Qux; @@ -295,20 +295,20 @@ impl foo::Bar for Foo { todo!() } } -", +"#, ) } #[test] fn add_custom_impl_for_unique_input() { check_assist( replace_derive_with_manual_impl, - " + r#" #[derive(Debu$0g)] struct Foo { bar: String, } - ", - " + "#, + r#" struct Foo { bar: String, } @@ -316,7 +316,7 @@ struct Foo { impl Debug for Foo { $0 } - ", + "#, ) } @@ -324,13 +324,13 @@ impl Debug for Foo { fn add_custom_impl_for_with_visibility_modifier() { check_assist( replace_derive_with_manual_impl, - " + r#" #[derive(Debug$0)] pub struct Foo { bar: String, } - ", - " + "#, + r#" pub struct Foo { bar: String, } @@ -338,7 +338,7 @@ pub struct Foo { impl Debug for Foo { $0 } - ", + "#, ) } @@ -346,18 +346,18 @@ impl Debug for Foo { fn add_custom_impl_when_multiple_inputs() { check_assist( replace_derive_with_manual_impl, - " + r#" #[derive(Display, Debug$0, Serialize)] struct Foo {} - ", - " + "#, + r#" #[derive(Display, Serialize)] struct Foo {} impl Debug for Foo { $0 } - ", + "#, ) } @@ -365,10 +365,10 @@ impl Debug for Foo { fn test_ignore_derive_macro_without_input() { check_assist_not_applicable( replace_derive_with_manual_impl, - " + r#" #[derive($0)] struct Foo {} - ", + "#, ) } @@ -376,18 +376,18 @@ struct Foo {} fn test_ignore_if_cursor_on_param() { check_assist_not_applicable( replace_derive_with_manual_impl, - " + r#" #[derive$0(Debug)] struct Foo {} - ", + "#, ); check_assist_not_applicable( replace_derive_with_manual_impl, - " + r#" #[derive(Debug)$0] struct Foo {} - ", + "#, ) } @@ -395,10 +395,10 @@ struct Foo {} fn test_ignore_if_not_derive() { check_assist_not_applicable( replace_derive_with_manual_impl, - " + r#" #[allow(non_camel_$0case_types)] struct Foo {} - ", + "#, ) } -- cgit v1.2.3