]> git.lizzy.rs Git - rust.git/commitdiff
Do not merge imports with different attributes
authorJesse Bakker <github@jessebakker.com>
Fri, 18 Dec 2020 13:30:56 +0000 (14:30 +0100)
committerJesse Bakker <github@jessebakker.com>
Fri, 18 Dec 2020 14:29:37 +0000 (15:29 +0100)
crates/ide_db/src/helpers/insert_use.rs
crates/ide_db/src/helpers/insert_use/tests.rs

index 9be36d59bdc34f2fa49354c1653405f8a7c4b2bf..d6b498be36dbd9205554dcc1c6d3ede3a84e1651 100644 (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)?;
index 9e194354e24a0d0f0deca5c58e87a54c2601cd61..a603fe87f2c692331b0ae43a0a36d02821a61416 100644 (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() {