Skip to content

Commit 03c177a

Browse files
Merge #6927
6927: Do not merge imports with different attributes r=lnicola a=Jesse-Bakker Fixes #6925 Co-authored-by: Jesse Bakker <[email protected]>
2 parents f4929fa + 700034b commit 03c177a

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

crates/ide_db/src/helpers/insert_use.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use syntax::{
99
ast::{
1010
self,
1111
edit::{AstNodeEdit, IndentLevel},
12-
make, AstNode, PathSegmentKind, VisibilityOwner,
12+
make, AstNode, AttrsOwner, PathSegmentKind, VisibilityOwner,
1313
},
1414
AstToken, InsertPosition, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxToken,
1515
};
@@ -180,6 +180,15 @@ fn eq_visibility(vis0: Option<ast::Visibility>, vis1: Option<ast::Visibility>) -
180180
}
181181
}
182182

183+
fn eq_attrs(
184+
attrs0: impl Iterator<Item = ast::Attr>,
185+
attrs1: impl Iterator<Item = ast::Attr>,
186+
) -> bool {
187+
let attrs0 = attrs0.map(|attr| attr.to_string());
188+
let attrs1 = attrs1.map(|attr| attr.to_string());
189+
attrs0.eq(attrs1)
190+
}
191+
183192
pub fn try_merge_imports(
184193
lhs: &ast::Use,
185194
rhs: &ast::Use,
@@ -189,6 +198,10 @@ pub fn try_merge_imports(
189198
if !eq_visibility(lhs.visibility(), rhs.visibility()) {
190199
return None;
191200
}
201+
if !eq_attrs(lhs.attrs(), rhs.attrs()) {
202+
return None;
203+
}
204+
192205
let lhs_tree = lhs.use_tree()?;
193206
let rhs_tree = rhs.use_tree()?;
194207
let merged = try_merge_trees(&lhs_tree, &rhs_tree, merge_behavior)?;

crates/ide_db/src/helpers/insert_use/tests.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,20 @@ use std::io;",
447447
)
448448
}
449449

450+
#[test]
451+
fn merge_groups_skip_attributed() {
452+
check_full(
453+
"std::io",
454+
r#"
455+
#[cfg(feature = "gated")] use std::fmt::{Result, Display};
456+
"#,
457+
r#"
458+
#[cfg(feature = "gated")] use std::fmt::{Result, Display};
459+
use std::io;
460+
"#,
461+
)
462+
}
463+
450464
#[test]
451465
#[ignore] // FIXME: Support this
452466
fn split_out_merge() {

0 commit comments

Comments
 (0)