Skip to content

Commit eae54b5

Browse files
bors[bot]Veykril
andauthored
Merge #6357
6357: Don't keep parens around with remove-dbg r=SomeoneToIgnore a=Veykril Fixes #6355 ~~This causes remove-dbg to not keep parentheses when it comes to ranges though due to ranges not having `DOT2` and `DOT2EQ` tokens but having two `DOT` tokens inside of macro invocations.~~ Co-authored-by: Lukas Wirth <[email protected]>
2 parents 91c1af3 + 551bf65 commit eae54b5

File tree

1 file changed

+84
-3
lines changed

1 file changed

+84
-3
lines changed

crates/assists/src/handlers/remove_dbg.rs

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ fn needs_parentheses_around_macro_contents(macro_contents: Vec<SyntaxElement>) -
9393
if macro_contents.len() < 2 {
9494
return false;
9595
}
96+
let mut macro_contents = macro_contents.into_iter().peekable();
9697
let mut unpaired_brackets_in_contents = Vec::new();
97-
for element in macro_contents {
98+
while let Some(element) = macro_contents.next() {
9899
match element.kind() {
99100
T!['('] | T!['['] | T!['{'] => unpaired_brackets_in_contents.push(element),
100101
T![')'] => {
@@ -118,8 +119,14 @@ fn needs_parentheses_around_macro_contents(macro_contents: Vec<SyntaxElement>) -
118119
symbol_kind => {
119120
let symbol_not_in_bracket = unpaired_brackets_in_contents.is_empty();
120121
if symbol_not_in_bracket
121-
&& symbol_kind != SyntaxKind::COLON
122-
&& symbol_kind.is_punct()
122+
&& symbol_kind != SyntaxKind::COLON // paths
123+
&& (symbol_kind != SyntaxKind::DOT // field/method access
124+
|| macro_contents // range expressions consist of two SyntaxKind::Dot in macro invocations
125+
.peek()
126+
.map(|element| element.kind() == SyntaxKind::DOT)
127+
.unwrap_or(false))
128+
&& symbol_kind != SyntaxKind::QUESTION // try operator
129+
&& (symbol_kind.is_punct() || symbol_kind == SyntaxKind::AS_KW)
123130
{
124131
return true;
125132
}
@@ -242,6 +249,25 @@ fn main() {
242249
check_assist(remove_dbg, r#"let res = <|>dbg![2 + 2] * 5"#, r#"let res = (2 + 2) * 5"#);
243250
}
244251

252+
#[test]
253+
fn test_remove_dbg_method_chaining() {
254+
check_assist(
255+
remove_dbg,
256+
r#"let res = <|>dbg!(foo().bar()).baz();"#,
257+
r#"let res = foo().bar().baz();"#,
258+
);
259+
check_assist(
260+
remove_dbg,
261+
r#"let res = <|>dbg!(foo.bar()).baz();"#,
262+
r#"let res = foo.bar().baz();"#,
263+
);
264+
}
265+
266+
#[test]
267+
fn test_remove_dbg_field_chaining() {
268+
check_assist(remove_dbg, r#"let res = <|>dbg!(foo.bar).baz;"#, r#"let res = foo.bar.baz;"#);
269+
}
270+
245271
#[test]
246272
fn test_remove_dbg_from_inside_fn() {
247273
check_assist_target(
@@ -280,4 +306,59 @@ fn main() {
280306
}"#,
281307
);
282308
}
309+
310+
#[test]
311+
fn test_remove_dbg_try_expr() {
312+
check_assist(
313+
remove_dbg,
314+
r#"let res = <|>dbg!(result?).foo();"#,
315+
r#"let res = result?.foo();"#,
316+
);
317+
}
318+
319+
#[test]
320+
fn test_remove_dbg_await_expr() {
321+
check_assist(
322+
remove_dbg,
323+
r#"let res = <|>dbg!(fut.await).foo();"#,
324+
r#"let res = fut.await.foo();"#,
325+
);
326+
}
327+
328+
#[test]
329+
fn test_remove_dbg_as_cast() {
330+
check_assist(
331+
remove_dbg,
332+
r#"let res = <|>dbg!(3 as usize).foo();"#,
333+
r#"let res = (3 as usize).foo();"#,
334+
);
335+
}
336+
337+
#[test]
338+
fn test_remove_dbg_index_expr() {
339+
check_assist(
340+
remove_dbg,
341+
r#"let res = <|>dbg!(array[3]).foo();"#,
342+
r#"let res = array[3].foo();"#,
343+
);
344+
check_assist(
345+
remove_dbg,
346+
r#"let res = <|>dbg!(tuple.3).foo();"#,
347+
r#"let res = tuple.3.foo();"#,
348+
);
349+
}
350+
351+
#[test]
352+
fn test_remove_dbg_range_expr() {
353+
check_assist(
354+
remove_dbg,
355+
r#"let res = <|>dbg!(foo..bar).foo();"#,
356+
r#"let res = (foo..bar).foo();"#,
357+
);
358+
check_assist(
359+
remove_dbg,
360+
r#"let res = <|>dbg!(foo..=bar).foo();"#,
361+
r#"let res = (foo..=bar).foo();"#,
362+
);
363+
}
283364
}

0 commit comments

Comments
 (0)