Disable remove unnecessary braces diagnotics for self imports

This commit is contained in:
AmrDeveloper 2023-07-05 16:34:04 +02:00
parent 0dd2c0d8d3
commit ce0239bd6a

View file

@ -1,6 +1,6 @@
use ide_db::{base_db::FileId, source_change::SourceChange}; use ide_db::{base_db::FileId, source_change::SourceChange};
use itertools::Itertools; use itertools::Itertools;
use syntax::{ast, AstNode, SyntaxNode, TextRange}; use syntax::{ast, AstNode, SyntaxKind, SyntaxNode, TextRange};
use text_edit::TextEdit; use text_edit::TextEdit;
use crate::{fix, Diagnostic, DiagnosticCode}; use crate::{fix, Diagnostic, DiagnosticCode};
@ -15,6 +15,11 @@ pub(crate) fn useless_braces(
) -> Option<()> { ) -> Option<()> {
let use_tree_list = ast::UseTreeList::cast(node.clone())?; let use_tree_list = ast::UseTreeList::cast(node.clone())?;
if let Some((single_use_tree,)) = use_tree_list.use_trees().collect_tuple() { if let Some((single_use_tree,)) = use_tree_list.use_trees().collect_tuple() {
// If there is a `self` inside the bracketed `use`, don't show diagnostic.
if single_use_tree.syntax().first_token().unwrap().kind() == SyntaxKind::SELF_KW {
return Some(());
}
// If there is a comment inside the bracketed `use`, // If there is a comment inside the bracketed `use`,
// assume it is a commented out module path and don't show diagnostic. // assume it is a commented out module path and don't show diagnostic.
if use_tree_list.has_inner_comment() { if use_tree_list.has_inner_comment() {
@ -91,6 +96,22 @@ mod a {
pub mod e {} pub mod e {}
} }
} }
"#,
);
check_diagnostics(
r#"
use a::{self};
mod a {
}
"#,
);
check_diagnostics(
r#"
use a::{self as cool_name};
mod a {
}
"#, "#,
); );
check_fix( check_fix(
@ -121,16 +142,6 @@ use a::{c$0};
r#" r#"
mod a { pub mod c {} } mod a { pub mod c {} }
use a::c; use a::c;
"#,
);
check_fix(
r#"
mod a {}
use a::{self$0};
"#,
r#"
mod a {}
use a;
"#, "#,
); );
check_fix( check_fix(