Skip to content

Commit 03b77b0

Browse files
committed
Fix stackoverflow in insert_use::recursive_merge
1 parent 0fb069c commit 03b77b0

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

crates/assists/src/handlers/merge_imports.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ mod tests {
7272

7373
use super::*;
7474

75+
#[test]
76+
fn test_merge_equal() {
77+
check_assist(
78+
merge_imports,
79+
r"
80+
use std::fmt<|>::{Display, Debug};
81+
use std::fmt::{Display, Debug};
82+
",
83+
r"
84+
use std::fmt::{Debug, Display};
85+
",
86+
)
87+
}
88+
7589
#[test]
7690
fn test_merge_first() {
7791
check_assist(

crates/assists/src/handlers/replace_qualified_name_with_use.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,23 @@ mod tests {
123123

124124
use super::*;
125125

126+
#[test]
127+
fn test_replace_already_imported() {
128+
check_assist(
129+
replace_qualified_name_with_use,
130+
r"use std::fs;
131+
132+
fn main() {
133+
std::f<|>s::Path
134+
}",
135+
r"use std::fs;
136+
137+
fn main() {
138+
fs::Path
139+
}",
140+
)
141+
}
142+
126143
#[test]
127144
fn test_replace_add_use_no_anchor() {
128145
check_assist(

crates/assists/src/utils/insert_use.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,15 @@ pub(crate) fn try_merge_trees(
173173
let rhs_path = rhs.path()?;
174174

175175
let (lhs_prefix, rhs_prefix) = common_prefix(&lhs_path, &rhs_path)?;
176-
let lhs = lhs.split_prefix(&lhs_prefix);
177-
let rhs = rhs.split_prefix(&rhs_prefix);
176+
let (lhs, rhs) = if is_simple_path(lhs)
177+
&& is_simple_path(rhs)
178+
&& lhs_path == lhs_prefix
179+
&& rhs_path == rhs_prefix
180+
{
181+
(lhs.clone(), rhs.clone())
182+
} else {
183+
(lhs.split_prefix(&lhs_prefix), rhs.split_prefix(&rhs_prefix))
184+
};
178185
recursive_merge(&lhs, &rhs, merge)
179186
}
180187

@@ -250,6 +257,10 @@ fn recursive_merge(
250257
use_trees.insert(idx, make::glob_use_tree());
251258
continue;
252259
}
260+
261+
if lhs_t.use_tree_list().is_none() && rhs_t.use_tree_list().is_none() {
262+
continue;
263+
}
253264
}
254265
let lhs = lhs_t.split_prefix(&lhs_prefix);
255266
let rhs = rhs_t.split_prefix(&rhs_prefix);
@@ -295,6 +306,10 @@ fn common_prefix(lhs: &ast::Path, rhs: &ast::Path) -> Option<(ast::Path, ast::Pa
295306
}
296307
}
297308

309+
fn is_simple_path(use_tree: &ast::UseTree) -> bool {
310+
use_tree.use_tree_list().is_none() && use_tree.star_token().is_none()
311+
}
312+
298313
fn path_is_self(path: &ast::Path) -> bool {
299314
path.segment().and_then(|seg| seg.self_token()).is_some() && path.qualifier().is_none()
300315
}
@@ -523,6 +538,11 @@ mod tests {
523538

524539
use test_utils::assert_eq_text;
525540

541+
#[test]
542+
fn insert_existing() {
543+
check_full("std::fs", "use std::fs;", "use std::fs;")
544+
}
545+
526546
#[test]
527547
fn insert_start() {
528548
check_none(

0 commit comments

Comments
 (0)