1
1
use itertools:: Itertools ;
2
2
use syntax:: {
3
3
ast:: { self , AstNode , AstToken } ,
4
- match_ast, NodeOrToken , SyntaxElement , TextRange , TextSize , T ,
4
+ match_ast, NodeOrToken , SyntaxElement , TextSize , T ,
5
5
} ;
6
6
7
7
use crate :: { AssistContext , AssistId , AssistKind , Assists } ;
@@ -41,7 +41,6 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
41
41
. ok ( ) ?;
42
42
43
43
let parent = macro_call. syntax ( ) . parent ( ) ?;
44
- let parent_is_let_stmt = ast:: LetStmt :: can_cast ( parent. kind ( ) ) ;
45
44
let ( range, text) = match & * input_expressions {
46
45
// dbg!()
47
46
[ ] => {
@@ -75,7 +74,6 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
75
74
(
76
75
ast:: Expr :: BoxExpr ( _) | ast:: Expr :: PrefixExpr ( _) | ast:: Expr :: RefExpr ( _) ,
77
76
ast:: Expr :: AwaitExpr ( _)
78
- | ast:: Expr :: BinExpr ( _)
79
77
| ast:: Expr :: CallExpr ( _)
80
78
| ast:: Expr :: CastExpr ( _)
81
79
| ast:: Expr :: FieldExpr ( _)
@@ -108,13 +106,7 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
108
106
)
109
107
}
110
108
// dbg!(expr0, expr1, ...)
111
- exprs => ( macro_call. syntax ( ) . text_range ( ) , format ! ( "({})" , exprs. iter( ) . format( "," ) ) ) ,
112
- } ;
113
-
114
- let range = if macro_call. semicolon_token ( ) . is_some ( ) && parent_is_let_stmt {
115
- TextRange :: new ( range. start ( ) , range. end ( ) - TextSize :: of ( ";" ) )
116
- } else {
117
- range
109
+ exprs => ( macro_call. syntax ( ) . text_range ( ) , format ! ( "({})" , exprs. iter( ) . format( ", " ) ) ) ,
118
110
} ;
119
111
120
112
acc. add ( AssistId ( "remove_dbg" , AssistKind :: Refactor ) , "Remove dbg!()" , range, |builder| {
@@ -143,18 +135,8 @@ mod tests {
143
135
#[ test]
144
136
fn test_remove_dbg ( ) {
145
137
check ( "$0dbg!(1 + 1)" , "1 + 1" ) ;
146
- check ( "dbg!$0(( 1 + 1)) " , "( 1 + 1) " ) ;
138
+ check ( "dbg!$0(1 + 1)" , "1 + 1" ) ;
147
139
check ( "dbg!(1 $0+ 1)" , "1 + 1" ) ;
148
- check ( "let _ = $0dbg!(1 + 1)" , "let _ = 1 + 1" ) ;
149
- check (
150
- "if let Some(_) = dbg!(n.$0checked_sub(4)) {}" ,
151
- "if let Some(_) = n.checked_sub(4) {}" ,
152
- ) ;
153
- check ( "$0dbg!(Foo::foo_test()).bar()" , "Foo::foo_test().bar()" ) ;
154
- }
155
-
156
- #[ test]
157
- fn test_remove_dbg_with_brackets_and_braces ( ) {
158
140
check ( "dbg![$01 + 1]" , "1 + 1" ) ;
159
141
check ( "dbg!{$01 + 1}" , "1 + 1" ) ;
160
142
}
@@ -167,100 +149,36 @@ mod tests {
167
149
}
168
150
169
151
#[ test]
170
- fn test_remove_dbg_keep_semicolon ( ) {
152
+ fn test_remove_dbg_keep_semicolon_in_let ( ) {
171
153
// https://github.com/rust-analyzer/rust-analyzer/issues/5129#issuecomment-651399779
172
- // not quite though
173
- // adding a comment at the end of the line makes
174
- // the ast::MacroCall to include the semicolon at the end
175
154
check (
176
155
r#"let res = $0dbg!(1 * 20); // needless comment"# ,
177
156
r#"let res = 1 * 20; // needless comment"# ,
178
157
) ;
179
- }
180
-
181
- #[ test]
182
- fn remove_dbg_from_non_leaf_simple_expression ( ) {
183
- check_assist (
184
- remove_dbg,
185
- r"
186
- fn main() {
187
- let mut a = 1;
188
- while dbg!$0(a) < 10000 {
189
- a += 1;
190
- }
191
- }
192
- " ,
193
- r"
194
- fn main() {
195
- let mut a = 1;
196
- while a < 10000 {
197
- a += 1;
198
- }
199
- }
200
- " ,
158
+ check ( r#"let res = $0dbg!(); // needless comment"# , r#"let res = (); // needless comment"# ) ;
159
+ check (
160
+ r#"let res = $0dbg!(1, 2); // needless comment"# ,
161
+ r#"let res = (1, 2); // needless comment"# ,
201
162
) ;
202
163
}
203
164
204
165
#[ test]
205
- fn test_remove_dbg_keep_expression ( ) {
206
- check ( r#"let res = $0dbg!(a + b).foo();"# , r#"let res = (a + b).foo();"# ) ;
207
- check ( r#"let res = $0dbg!(2 + 2) * 5"# , r#"let res = (2 + 2) * 5"# ) ;
208
- check ( r#"let res = $0dbg![2 + 2] * 5"# , r#"let res = (2 + 2) * 5"# ) ;
209
- }
210
-
211
- #[ test]
212
- fn test_remove_dbg_method_chaining ( ) {
213
- check ( r#"let res = $0dbg!(foo().bar()).baz();"# , r#"let res = foo().bar().baz();"# ) ;
214
- check ( r#"let res = $0dbg!(foo.bar()).baz();"# , r#"let res = foo.bar().baz();"# ) ;
215
- }
216
-
217
- #[ test]
218
- fn test_remove_dbg_field_chaining ( ) {
219
- check ( r#"let res = $0dbg!(foo.bar).baz;"# , r#"let res = foo.bar.baz;"# ) ;
220
- }
221
-
222
- #[ test]
223
- fn test_remove_dbg_from_inside_fn ( ) {
224
- check_assist (
225
- remove_dbg,
226
- r#"
227
- fn square(x: u32) -> u32 {
228
- x * x
229
- }
230
-
231
- fn main() {
232
- let x = square(dbg$0!(5 + 10));
233
- println!("{}", x);
234
- }"# ,
235
- r#"
236
- fn square(x: u32) -> u32 {
237
- x * x
238
- }
239
-
240
- fn main() {
241
- let x = square(5 + 10);
242
- println!("{}", x);
243
- }"# ,
244
- ) ;
166
+ fn test_remove_dbg_cast_cast ( ) {
167
+ check ( r#"let res = $0dbg!(x as u32) as u32;"# , r#"let res = x as u32 as u32;"# ) ;
245
168
}
246
169
247
170
#[ test]
248
- fn test_remove_dbg_try_expr ( ) {
249
- check ( r#"let res = $0dbg!(result?).foo();"# , r#"let res = result?.foo();"# ) ;
171
+ fn test_remove_dbg_prefix ( ) {
172
+ check ( r#"let res = $0dbg!(&result).foo();"# , r#"let res = (&result).foo();"# ) ;
173
+ check ( r#"let res = &$0dbg!(&result);"# , r#"let res = &&result;"# ) ;
174
+ check ( r#"let res = $0dbg!(!result) && true;"# , r#"let res = !result && true;"# ) ;
250
175
}
251
176
252
177
#[ test]
253
- fn test_remove_dbg_await_expr ( ) {
178
+ fn test_remove_dbg_post_expr ( ) {
254
179
check ( r#"let res = $0dbg!(fut.await).foo();"# , r#"let res = fut.await.foo();"# ) ;
255
- }
256
-
257
- #[ test]
258
- fn test_remove_dbg_as_cast ( ) {
259
- check ( r#"let res = $0dbg!(3 as usize).foo();"# , r#"let res = (3 as usize).foo();"# ) ;
260
- }
261
-
262
- #[ test]
263
- fn test_remove_dbg_index_expr ( ) {
180
+ check ( r#"let res = $0dbg!(result?).foo();"# , r#"let res = result?.foo();"# ) ;
181
+ check ( r#"let res = $0dbg!(foo as u32).foo();"# , r#"let res = (foo as u32).foo();"# ) ;
264
182
check ( r#"let res = $0dbg!(array[3]).foo();"# , r#"let res = array[3].foo();"# ) ;
265
183
check ( r#"let res = $0dbg!(tuple.3).foo();"# , r#"let res = tuple.3.foo();"# ) ;
266
184
}
@@ -271,46 +189,6 @@ fn main() {
271
189
check ( r#"let res = $0dbg!(foo..=bar).foo();"# , r#"let res = (foo..=bar).foo();"# ) ;
272
190
}
273
191
274
- #[ test]
275
- fn test_remove_dbg_followed_by_block ( ) {
276
- check_assist (
277
- remove_dbg,
278
- r#"fn foo() {
279
- if $0dbg!(x || y) {}
280
- }"# ,
281
- r#"fn foo() {
282
- if x || y {}
283
- }"# ,
284
- ) ;
285
- check_assist (
286
- remove_dbg,
287
- r#"fn foo() {
288
- while let foo = $0dbg!(&x) {}
289
- }"# ,
290
- r#"fn foo() {
291
- while let foo = &x {}
292
- }"# ,
293
- ) ;
294
- check_assist (
295
- remove_dbg,
296
- r#"fn foo() {
297
- if let foo = $0dbg!(&x) {}
298
- }"# ,
299
- r#"fn foo() {
300
- if let foo = &x {}
301
- }"# ,
302
- ) ;
303
- check_assist (
304
- remove_dbg,
305
- r#"fn foo() {
306
- match $0dbg!(&x) {}
307
- }"# ,
308
- r#"fn foo() {
309
- match &x {}
310
- }"# ,
311
- ) ;
312
- }
313
-
314
192
#[ test]
315
193
fn test_remove_empty_dbg ( ) {
316
194
check_assist ( remove_dbg, r#"fn foo() { $0dbg!(); }"# , r#"fn foo() { }"# ) ;
@@ -354,4 +232,10 @@ fn foo() {
354
232
}"# ,
355
233
) ;
356
234
}
235
+
236
+ #[ test]
237
+ fn test_remove_multi_dbg ( ) {
238
+ check ( r#"$0dbg!(0, 1)"# , r#"(0, 1)"# ) ;
239
+ check ( r#"$0dbg!(0, (1, 2))"# , r#"(0, (1, 2))"# ) ;
240
+ }
357
241
}
0 commit comments