Skip to content

Commit c5bcd56

Browse files
committed
Cleanup tests
1 parent da00208 commit c5bcd56

File tree

1 file changed

+23
-139
lines changed

1 file changed

+23
-139
lines changed

crates/ide_assists/src/handlers/remove_dbg.rs

Lines changed: 23 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use itertools::Itertools;
22
use syntax::{
33
ast::{self, AstNode, AstToken},
4-
match_ast, NodeOrToken, SyntaxElement, TextRange, TextSize, T,
4+
match_ast, NodeOrToken, SyntaxElement, TextSize, T,
55
};
66

77
use crate::{AssistContext, AssistId, AssistKind, Assists};
@@ -41,7 +41,6 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
4141
.ok()?;
4242

4343
let parent = macro_call.syntax().parent()?;
44-
let parent_is_let_stmt = ast::LetStmt::can_cast(parent.kind());
4544
let (range, text) = match &*input_expressions {
4645
// dbg!()
4746
[] => {
@@ -75,7 +74,6 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
7574
(
7675
ast::Expr::BoxExpr(_) | ast::Expr::PrefixExpr(_) | ast::Expr::RefExpr(_),
7776
ast::Expr::AwaitExpr(_)
78-
| ast::Expr::BinExpr(_)
7977
| ast::Expr::CallExpr(_)
8078
| ast::Expr::CastExpr(_)
8179
| ast::Expr::FieldExpr(_)
@@ -108,13 +106,7 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
108106
)
109107
}
110108
// 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(", "))),
118110
};
119111

120112
acc.add(AssistId("remove_dbg", AssistKind::Refactor), "Remove dbg!()", range, |builder| {
@@ -143,18 +135,8 @@ mod tests {
143135
#[test]
144136
fn test_remove_dbg() {
145137
check("$0dbg!(1 + 1)", "1 + 1");
146-
check("dbg!$0((1 + 1))", "(1 + 1)");
138+
check("dbg!$0(1 + 1)", "1 + 1");
147139
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() {
158140
check("dbg![$01 + 1]", "1 + 1");
159141
check("dbg!{$01 + 1}", "1 + 1");
160142
}
@@ -167,100 +149,36 @@ mod tests {
167149
}
168150

169151
#[test]
170-
fn test_remove_dbg_keep_semicolon() {
152+
fn test_remove_dbg_keep_semicolon_in_let() {
171153
// 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
175154
check(
176155
r#"let res = $0dbg!(1 * 20); // needless comment"#,
177156
r#"let res = 1 * 20; // needless comment"#,
178157
);
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"#,
201162
);
202163
}
203164

204165
#[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;"#);
245168
}
246169

247170
#[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;"#);
250175
}
251176

252177
#[test]
253-
fn test_remove_dbg_await_expr() {
178+
fn test_remove_dbg_post_expr() {
254179
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();"#);
264182
check(r#"let res = $0dbg!(array[3]).foo();"#, r#"let res = array[3].foo();"#);
265183
check(r#"let res = $0dbg!(tuple.3).foo();"#, r#"let res = tuple.3.foo();"#);
266184
}
@@ -271,46 +189,6 @@ fn main() {
271189
check(r#"let res = $0dbg!(foo..=bar).foo();"#, r#"let res = (foo..=bar).foo();"#);
272190
}
273191

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-
314192
#[test]
315193
fn test_remove_empty_dbg() {
316194
check_assist(remove_dbg, r#"fn foo() { $0dbg!(); }"#, r#"fn foo() { }"#);
@@ -354,4 +232,10 @@ fn foo() {
354232
}"#,
355233
);
356234
}
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+
}
357241
}

0 commit comments

Comments
 (0)