about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-12-30 07:45:33 +0000
committerGitHub <noreply@github.com>2020-12-30 07:45:33 +0000
commite7d2b5888b8a7e632ae9080108ccbc450316fd86 (patch)
tree721f9183ad55b80a77b1ae787e6e5f299def28ef
parent848e817f603ed12e065bc3057d12e04b481fb5bb (diff)
parentddbf484acf15efd73b61ac80a941730b507c01de (diff)
Merge #7088
7088: Smarter bracketed `use` diagnostic r=lnicola a=AdnoC

Closes https://github.com/rust-analyzer/rust-analyzer/issues/4531

Makes it so that if a bracketed use statement contains a comment inside the braces, no "Unnecessary braces in use statement" diagnostic is shown.

Co-authored-by: AdnoC <adam.r.cutler@gmail.com>
-rw-r--r--crates/ide/src/diagnostics.rs22
-rw-r--r--crates/syntax/src/ast/node_ext.rs8
2 files changed, 30 insertions, 0 deletions
diff --git a/crates/ide/src/diagnostics.rs b/crates/ide/src/diagnostics.rs
index 03827375033..79d126ff2a5 100644
--- a/crates/ide/src/diagnostics.rs
+++ b/crates/ide/src/diagnostics.rs
@@ -199,6 +199,12 @@ fn check_unnecessary_braces_in_use_statement(
 ) -> Option<()> {
     let use_tree_list = ast::UseTreeList::cast(node.clone())?;
     if let Some((single_use_tree,)) = use_tree_list.use_trees().collect_tuple() {
+        // If there is a comment inside the bracketed `use`,
+        // assume it is a commented out module path and don't show diagnostic.
+        if use_tree_list.has_inner_comment() {
+            return Some(());
+        }
+
         let use_range = use_tree_list.syntax().text_range();
         let edit =
             text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(&single_use_tree)
@@ -638,6 +644,22 @@ mod a {
 }
 "#,
         );
+        check_no_diagnostics(
+            r#"
+use a;
+use a::{
+    c,
+    // d::e
+};
+
+mod a {
+    mod c {}
+    mod d {
+        mod e {}
+    }
+}
+"#,
+        );
         check_fix(
             r"
             mod b {}
diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs
index c45cb514a0f..2aa472fb494 100644
--- a/crates/syntax/src/ast/node_ext.rs
+++ b/crates/syntax/src/ast/node_ext.rs
@@ -193,6 +193,14 @@ impl ast::UseTreeList {
             .and_then(ast::UseTree::cast)
             .expect("UseTreeLists are always nested in UseTrees")
     }
+
+    pub fn has_inner_comment(&self) -> bool {
+        self.syntax()
+            .children_with_tokens()
+            .filter_map(|it| it.into_token())
+            .find_map(ast::Comment::cast)
+            .is_some()
+    }
 }
 
 impl ast::Impl {