summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-11-24 15:16:36 +0000
committerbors <bors@rust-lang.org>2022-11-24 15:16:36 +0000
commit63a676eedfcad1f751fd6fbe2a43e5ebe2cc3c86 (patch)
treeb77a58c493fe060c5a345e74d678fb2fe1c2ce2e
parent81d26e730eccb3c7b51d5d36b06950bfe84a81f6 (diff)
parent95b4a7487b4055d951b59967caffcdc92a733c84 (diff)
downloadrust-63a676eedfcad1f751fd6fbe2a43e5ebe2cc3c86.tar.gz
Auto merge of #13576 - Bben01:supress_missing_impl_inside_block, r=jonas-schievink
Suppress "Implement default members" inside contained items Fixes #13561
-rw-r--r--crates/ide-assists/src/handlers/add_missing_impl_members.rs99
1 files changed, 99 insertions, 0 deletions
diff --git a/crates/ide-assists/src/handlers/add_missing_impl_members.rs b/crates/ide-assists/src/handlers/add_missing_impl_members.rs
index 2b3793659cf..161bcc5c8da 100644
--- a/crates/ide-assists/src/handlers/add_missing_impl_members.rs
+++ b/crates/ide-assists/src/handlers/add_missing_impl_members.rs
@@ -107,6 +107,14 @@ fn add_missing_impl_members_inner(
) -> Option<()> {
let _p = profile::span("add_missing_impl_members_inner");
let impl_def = ctx.find_node_at_offset::<ast::Impl>()?;
+
+ if ctx.token_at_offset().all(|t| {
+ t.parent_ancestors()
+ .any(|s| ast::BlockExpr::can_cast(s.kind()) || ast::ParamList::can_cast(s.kind()))
+ }) {
+ return None;
+ }
+
let target_scope = ctx.sema.scope(impl_def.syntax())?;
let trait_ = resolve_target_trait(&ctx.sema, &impl_def)?;
@@ -1343,4 +1351,95 @@ impl PartialEq for SomeStruct {
"#,
);
}
+
+ #[test]
+ fn test_ignore_function_body() {
+ check_assist_not_applicable(
+ add_missing_default_members,
+ r#"
+trait Trait {
+ type X;
+ fn foo(&self);
+ fn bar(&self) {}
+}
+
+impl Trait for () {
+ type X = u8;
+ fn foo(&self) {$0
+ let x = 5;
+ }
+}"#,
+ )
+ }
+
+ #[test]
+ fn test_ignore_param_list() {
+ check_assist_not_applicable(
+ add_missing_impl_members,
+ r#"
+trait Trait {
+ type X;
+ fn foo(&self);
+ fn bar(&self);
+}
+
+impl Trait for () {
+ type X = u8;
+ fn foo(&self$0) {
+ let x = 5;
+ }
+}"#,
+ )
+ }
+
+ #[test]
+ fn test_ignore_scope_inside_function() {
+ check_assist_not_applicable(
+ add_missing_impl_members,
+ r#"
+trait Trait {
+ type X;
+ fn foo(&self);
+ fn bar(&self);
+}
+
+impl Trait for () {
+ type X = u8;
+ fn foo(&self) {
+ let x = async {$0 5 };
+ }
+}"#,
+ )
+ }
+
+ #[test]
+ fn test_apply_outside_function() {
+ check_assist(
+ add_missing_default_members,
+ r#"
+trait Trait {
+ type X;
+ fn foo(&self);
+ fn bar(&self) {}
+}
+
+impl Trait for () {
+ type X = u8;
+ fn foo(&self)$0 {}
+}"#,
+ r#"
+trait Trait {
+ type X;
+ fn foo(&self);
+ fn bar(&self) {}
+}
+
+impl Trait for () {
+ type X = u8;
+ fn foo(&self) {}
+
+ $0fn bar(&self) {}
+}"#,
+ )
+ }
}