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>
This commit is contained in:
bors[bot] 2020-12-30 07:45:33 +00:00 committed by GitHub
commit e7d2b5888b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View File

@ -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)
@ -630,6 +636,22 @@ fn test_check_unnecessary_braces_in_use_statement() {
use a;
use a::{c, d::e};
mod a {
mod c {}
mod d {
mod e {}
}
}
"#,
);
check_no_diagnostics(
r#"
use a;
use a::{
c,
// d::e
};
mod a {
mod c {}
mod d {

View File

@ -193,6 +193,14 @@ pub fn parent_use_tree(&self) -> ast::UseTree {
.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 {