From 20186cdf89a0b5027228ea01e0322bc4dac0456d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 15 Nov 2019 22:48:07 +0300 Subject: Fix add-new assist --- crates/ra_assists/src/assists/add_new.rs | 69 +++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 5 deletions(-) (limited to 'crates/ra_assists/src') diff --git a/crates/ra_assists/src/assists/add_new.rs b/crates/ra_assists/src/assists/add_new.rs index a8839cfba..2038afdc6 100644 --- a/crates/ra_assists/src/assists/add_new.rs +++ b/crates/ra_assists/src/assists/add_new.rs @@ -158,9 +158,12 @@ fn find_struct_impl( let same_ty = blk.target_ty(db) == struct_ty; let not_trait_impl = blk.target_trait(db).is_none(); - found_new_fn = has_new_fn(impl_blk); + if !(same_ty && not_trait_impl) { + return false; + } - same_ty && not_trait_impl + found_new_fn = has_new_fn(impl_blk); + true }); if found_new_fn { @@ -186,9 +189,10 @@ fn has_new_fn(imp: &ast::ImplBlock) -> bool { #[cfg(test)] mod tests { - use super::*; use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target}; + use super::*; + #[test] #[rustfmt::skip] fn test_add_new() { @@ -345,7 +349,7 @@ struct Foo {<|>} impl Foo { fn new() -> Self { Self - } + } }", ); @@ -357,7 +361,7 @@ struct Foo {<|>} impl Foo { fn New() -> Self { Self - } + } }", ); } @@ -376,4 +380,59 @@ struct EvenMoreIrrelevant; struct Foo<'a, T: Foo<'a>> {}", ); } + + #[test] + fn test_unrelated_new() { + check_assist( + add_new, + r##" +pub struct AstId { + file_id: HirFileId, + file_ast_id: FileAstId, +} + +impl AstId { + pub fn new(file_id: HirFileId, file_ast_id: FileAstId) -> AstId { + AstId { file_id, file_ast_id } + } +} + +pub struct Source { + pub file_id: HirFileId,<|> + pub ast: T, +} + +impl Source { + pub fn map U, U>(self, f: F) -> Source { + Source { file_id: self.file_id, ast: f(self.ast) } + } +} +"##, + r##" +pub struct AstId { + file_id: HirFileId, + file_ast_id: FileAstId, +} + +impl AstId { + pub fn new(file_id: HirFileId, file_ast_id: FileAstId) -> AstId { + AstId { file_id, file_ast_id } + } +} + +pub struct Source { + pub file_id: HirFileId, + pub ast: T, +} + +impl Source { + pub fn new(file_id: HirFileId, ast: T) -> Self { Self { file_id, ast } }<|> + + pub fn map U, U>(self, f: F) -> Source { + Source { file_id: self.file_id, ast: f(self.ast) } + } +} +"##, + ); + } } -- cgit v1.2.3