Skip to content

Commit 48bb4bf

Browse files
bors[bot]Veykril
andauthored
Merge #6595
6595: Don't wrap parens around expr in remove_dbg assist if its in conditions r=Veykril a=Veykril If the expr in the `dbg!` macro consists of multiple elements it won't remove the parentheses if its in a `match` or condition-using construct, as these are followed by siblings causing the `is_leaf` check to fail. Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2 parents 66db0d3 + 5d39f6a commit 48bb4bf

File tree

1 file changed

+64
-7
lines changed

1 file changed

+64
-7
lines changed

crates/assists/src/handlers/remove_dbg.rs

+64-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use syntax::{
22
ast::{self, AstNode},
3-
SyntaxElement, SyntaxKind, TextRange, TextSize, T,
3+
match_ast, SyntaxElement, SyntaxKind, TextRange, TextSize, T,
44
};
55

66
use crate::{AssistContext, AssistId, AssistKind, Assists};
@@ -49,12 +49,29 @@ fn adjusted_macro_contents(macro_call: &ast::MacroCall) -> Option<String> {
4949
macro_text_with_brackets.len() - TextSize::of(')'),
5050
));
5151

52-
let is_leaf = macro_call.syntax().next_sibling().is_none();
53-
Some(if !is_leaf && needs_parentheses_around_macro_contents(contents) {
54-
format!("({})", macro_text_in_brackets)
55-
} else {
56-
macro_text_in_brackets.to_string()
57-
})
52+
Some(
53+
if !is_leaf_or_control_flow_expr(macro_call)
54+
&& needs_parentheses_around_macro_contents(contents)
55+
{
56+
format!("({})", macro_text_in_brackets)
57+
} else {
58+
macro_text_in_brackets.to_string()
59+
},
60+
)
61+
}
62+
63+
fn is_leaf_or_control_flow_expr(macro_call: &ast::MacroCall) -> bool {
64+
macro_call.syntax().next_sibling().is_none()
65+
|| match macro_call.syntax().parent() {
66+
Some(parent) => match_ast! {
67+
match parent {
68+
ast::Condition(_it) => true,
69+
ast::MatchExpr(_it) => true,
70+
_ => false,
71+
}
72+
},
73+
None => false,
74+
}
5875
}
5976

6077
/// Verifies that the given macro_call actually matches the given name
@@ -361,4 +378,44 @@ fn main() {
361378
r#"let res = (foo..=bar).foo();"#,
362379
);
363380
}
381+
382+
#[test]
383+
fn test_remove_dbg_followed_by_block() {
384+
check_assist(
385+
remove_dbg,
386+
r#"fn foo() {
387+
if <|>dbg!(x || y) {}
388+
}"#,
389+
r#"fn foo() {
390+
if x || y {}
391+
}"#,
392+
);
393+
check_assist(
394+
remove_dbg,
395+
r#"fn foo() {
396+
while let foo = <|>dbg!(&x) {}
397+
}"#,
398+
r#"fn foo() {
399+
while let foo = &x {}
400+
}"#,
401+
);
402+
check_assist(
403+
remove_dbg,
404+
r#"fn foo() {
405+
if let foo = <|>dbg!(&x) {}
406+
}"#,
407+
r#"fn foo() {
408+
if let foo = &x {}
409+
}"#,
410+
);
411+
check_assist(
412+
remove_dbg,
413+
r#"fn foo() {
414+
match <|>dbg!(&x) {}
415+
}"#,
416+
r#"fn foo() {
417+
match &x {}
418+
}"#,
419+
);
420+
}
364421
}

0 commit comments

Comments
 (0)