fix: Don't drop tree when the other has self

This commit is contained in:
DropDemBits 2022-02-14 18:46:16 -05:00
parent 014d3ef1a4
commit df2eb3c7cb
No known key found for this signature in database
GPG Key ID: 894910BF83E70347
2 changed files with 71 additions and 3 deletions

View File

@ -252,6 +252,75 @@ use std::{fmt::{Display, Debug}};
);
}
#[test]
fn test_merge_with_nested_self_item() {
check_assist(
merge_imports,
r"
use std$0::{fmt::{Write, Display}};
use std::{fmt::{self, Debug}};
",
r"
use std::{fmt::{Write, Display, self, Debug}};
",
);
}
#[test]
fn test_merge_with_nested_self_item2() {
check_assist(
merge_imports,
r"
use std$0::{fmt::{self, Debug}};
use std::{fmt::{Write, Display}};
",
r"
use std::{fmt::{self, Debug, Write, Display}};
",
);
}
#[test]
fn test_merge_self_with_nested_self_item() {
check_assist(
merge_imports,
r"
use std::{fmt$0::{self, Debug}, fmt::{Write, Display}};
",
r"
use std::{fmt::{self, Debug, Write, Display}};
",
);
}
#[test]
fn test_merge_nested_self_and_empty() {
check_assist(
merge_imports,
r"
use foo::$0{bar::{self}};
use foo::{bar};
",
r"
use foo::{bar::{self}};
",
)
}
#[test]
fn test_merge_nested_empty_and_self() {
check_assist(
merge_imports,
r"
use foo::$0{bar};
use foo::{bar::{self}};
",
r"
use foo::{bar::{self}};
",
)
}
#[test]
fn test_merge_single_wildcard_diff_prefixes() {
check_assist(

View File

@ -115,11 +115,10 @@ fn recursive_merge(lhs: &ast::UseTree, rhs: &ast::UseTree, merge: MergeBehavior)
let tree_contains_self = |tree: &ast::UseTree| {
tree.use_tree_list()
.map(|tree_list| tree_list.use_trees().any(|it| tree_is_self(&it)))
.unwrap_or(false)
};
match (tree_contains_self(lhs_t), tree_contains_self(&rhs_t)) {
(true, false) => continue,
(false, true) => {
(Some(true), None) => continue,
(None, Some(true)) => {
ted::replace(lhs_t.syntax(), rhs_t.syntax());
*lhs_t = rhs_t;
continue;