Do not merge imports with different attributes

This commit is contained in:
Jesse Bakker 2020-12-18 14:30:56 +01:00
parent f4929fa9cc
commit 700034bd5a
2 changed files with 28 additions and 1 deletions

View File

@ -9,7 +9,7 @@
ast::{
self,
edit::{AstNodeEdit, IndentLevel},
make, AstNode, PathSegmentKind, VisibilityOwner,
make, AstNode, AttrsOwner, PathSegmentKind, VisibilityOwner,
},
AstToken, InsertPosition, NodeOrToken, SyntaxElement, SyntaxNode, SyntaxToken,
};
@ -180,6 +180,15 @@ fn eq_visibility(vis0: Option<ast::Visibility>, vis1: Option<ast::Visibility>) -
}
}
fn eq_attrs(
attrs0: impl Iterator<Item = ast::Attr>,
attrs1: impl Iterator<Item = ast::Attr>,
) -> bool {
let attrs0 = attrs0.map(|attr| attr.to_string());
let attrs1 = attrs1.map(|attr| attr.to_string());
attrs0.eq(attrs1)
}
pub fn try_merge_imports(
lhs: &ast::Use,
rhs: &ast::Use,
@ -189,6 +198,10 @@ pub fn try_merge_imports(
if !eq_visibility(lhs.visibility(), rhs.visibility()) {
return None;
}
if !eq_attrs(lhs.attrs(), rhs.attrs()) {
return None;
}
let lhs_tree = lhs.use_tree()?;
let rhs_tree = rhs.use_tree()?;
let merged = try_merge_trees(&lhs_tree, &rhs_tree, merge_behavior)?;

View File

@ -447,6 +447,20 @@ fn merge_groups_skip_pub_crate() {
)
}
#[test]
fn merge_groups_skip_attributed() {
check_full(
"std::io",
r#"
#[cfg(feature = "gated")] use std::fmt::{Result, Display};
"#,
r#"
#[cfg(feature = "gated")] use std::fmt::{Result, Display};
use std::io;
"#,
)
}
#[test]
#[ignore] // FIXME: Support this
fn split_out_merge() {