1
1
use ide_db:: { base_db:: FileId , source_change:: SourceChange } ;
2
2
use itertools:: Itertools ;
3
- use syntax:: { ast, AstNode , SyntaxNode , TextRange } ;
3
+ use syntax:: { ast, AstNode , SyntaxNode } ;
4
4
use text_edit:: TextEdit ;
5
5
6
6
use crate :: { fix, Diagnostic , DiagnosticCode } ;
@@ -15,20 +15,23 @@ pub(crate) fn useless_braces(
15
15
) -> Option < ( ) > {
16
16
let use_tree_list = ast:: UseTreeList :: cast ( node. clone ( ) ) ?;
17
17
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
+
18
23
// If there is a comment inside the bracketed `use`,
19
24
// assume it is a commented out module path and don't show diagnostic.
20
25
if use_tree_list. has_inner_comment ( ) {
21
26
return Some ( ( ) ) ;
22
27
}
23
28
24
29
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 ( ) ;
32
35
33
36
acc. push (
34
37
Diagnostic :: new (
@@ -48,19 +51,12 @@ pub(crate) fn useless_braces(
48
51
Some ( ( ) )
49
52
}
50
53
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
-
61
54
#[ cfg( test) ]
62
55
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
+ } ;
64
60
65
61
#[ test]
66
62
fn test_check_unnecessary_braces_in_use_statement ( ) {
@@ -91,6 +87,32 @@ mod a {
91
87
pub mod e {}
92
88
}
93
89
}
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};
94
116
"# ,
95
117
) ;
96
118
check_fix (
@@ -121,16 +143,6 @@ use a::{c$0};
121
143
r#"
122
144
mod a { pub mod c {} }
123
145
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;
134
146
"# ,
135
147
) ;
136
148
check_fix (
0 commit comments