Skip to content

Commit 3ea31e0

Browse files
committed
Auto merge of rust-lang#15216 - AmrDeveloper:stop_diagnostics_for_self, r=lowr
Disable remove unnecessary braces diagnotics for self imports Disable `remove unnecessary braces` diagnostic if the there is a `self` inside the bracketed `use` Fix rust-lang#15191
2 parents e95644e + 54e8973 commit 3ea31e0

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

crates/ide-diagnostics/src/handlers/useless_braces.rs

+41-29
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use ide_db::{base_db::FileId, source_change::SourceChange};
22
use itertools::Itertools;
3-
use syntax::{ast, AstNode, SyntaxNode, TextRange};
3+
use syntax::{ast, AstNode, SyntaxNode};
44
use text_edit::TextEdit;
55

66
use crate::{fix, Diagnostic, DiagnosticCode};
@@ -15,20 +15,23 @@ pub(crate) fn useless_braces(
1515
) -> Option<()> {
1616
let use_tree_list = ast::UseTreeList::cast(node.clone())?;
1717
if let Some((single_use_tree,)) = use_tree_list.use_trees().collect_tuple() {
18+
// If there is a `self` inside the bracketed `use`, don't show diagnostic.
19+
if single_use_tree.path()?.segment()?.self_token().is_some() {
20+
return Some(());
21+
}
22+
1823
// If there is a comment inside the bracketed `use`,
1924
// assume it is a commented out module path and don't show diagnostic.
2025
if use_tree_list.has_inner_comment() {
2126
return Some(());
2227
}
2328

2429
let use_range = use_tree_list.syntax().text_range();
25-
let edit = remove_braces(&single_use_tree).unwrap_or_else(|| {
26-
let to_replace = single_use_tree.syntax().text().to_string();
27-
let mut edit_builder = TextEdit::builder();
28-
edit_builder.delete(use_range);
29-
edit_builder.insert(use_range.start(), to_replace);
30-
edit_builder.finish()
31-
});
30+
let to_replace = single_use_tree.syntax().text().to_string();
31+
let mut edit_builder = TextEdit::builder();
32+
edit_builder.delete(use_range);
33+
edit_builder.insert(use_range.start(), to_replace);
34+
let edit = edit_builder.finish();
3235

3336
acc.push(
3437
Diagnostic::new(
@@ -48,19 +51,12 @@ pub(crate) fn useless_braces(
4851
Some(())
4952
}
5053

51-
fn remove_braces(single_use_tree: &ast::UseTree) -> Option<TextEdit> {
52-
let use_tree_list_node = single_use_tree.syntax().parent()?;
53-
if single_use_tree.path()?.segment()?.self_token().is_some() {
54-
let start = use_tree_list_node.prev_sibling_or_token()?.text_range().start();
55-
let end = use_tree_list_node.text_range().end();
56-
return Some(TextEdit::delete(TextRange::new(start, end)));
57-
}
58-
None
59-
}
60-
6154
#[cfg(test)]
6255
mod tests {
63-
use crate::tests::{check_diagnostics, check_fix};
56+
use crate::{
57+
tests::{check_diagnostics, check_diagnostics_with_config, check_fix},
58+
DiagnosticsConfig,
59+
};
6460

6561
#[test]
6662
fn test_check_unnecessary_braces_in_use_statement() {
@@ -91,6 +87,32 @@ mod a {
9187
pub mod e {}
9288
}
9389
}
90+
"#,
91+
);
92+
check_diagnostics(
93+
r#"
94+
use a::{self};
95+
96+
mod a {
97+
}
98+
"#,
99+
);
100+
check_diagnostics(
101+
r#"
102+
use a::{self as cool_name};
103+
104+
mod a {
105+
}
106+
"#,
107+
);
108+
109+
let mut config = DiagnosticsConfig::test_sample();
110+
config.disabled.insert("syntax-error".to_string());
111+
check_diagnostics_with_config(
112+
config,
113+
r#"
114+
mod a { pub mod b {} }
115+
use a::{b::self};
94116
"#,
95117
);
96118
check_fix(
@@ -121,16 +143,6 @@ use a::{c$0};
121143
r#"
122144
mod a { pub mod c {} }
123145
use a::c;
124-
"#,
125-
);
126-
check_fix(
127-
r#"
128-
mod a {}
129-
use a::{self$0};
130-
"#,
131-
r#"
132-
mod a {}
133-
use a;
134146
"#,
135147
);
136148
check_fix(

0 commit comments

Comments
 (0)