Skip to content

Commit c862346

Browse files
Merge #5981
5981: Properly preserve macro braces during dbg! removal r=jonas-schievink a=SomeoneToIgnore Do `dbg![2 + 2] * 5` -> `(2 + 2) * 5` This PR also implicitly handles the `{}` case too, but I decided not to add it into the test case since it's a compiler error on the latest stable rustc. Co-authored-by: Kirill Bulatov <[email protected]>
2 parents 199ebf1 + 779ea2e commit c862346

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

crates/assists/src/handlers/remove_dbg.rs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,18 @@ pub(crate) fn remove_dbg(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
4343

4444
fn adjusted_macro_contents(macro_call: &ast::MacroCall) -> Option<String> {
4545
let contents = get_valid_macrocall_contents(&macro_call, "dbg")?;
46-
let is_leaf = macro_call.syntax().next_sibling().is_none();
4746
let macro_text_with_brackets = macro_call.token_tree()?.syntax().text();
48-
let slice_index = if is_leaf || !needs_parentheses_around_macro_contents(contents) {
49-
TextRange::new(TextSize::of('('), macro_text_with_brackets.len() - TextSize::of(')'))
47+
let macro_text_in_brackets = macro_text_with_brackets.slice(TextRange::new(
48+
TextSize::of('('),
49+
macro_text_with_brackets.len() - TextSize::of(')'),
50+
));
51+
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)
5055
} else {
51-
// leave parenthesis around macro contents to preserve the semantics
52-
TextRange::up_to(macro_text_with_brackets.len())
53-
};
54-
Some(macro_text_with_brackets.slice(slice_index).to_string())
56+
macro_text_in_brackets.to_string()
57+
})
5558
}
5659

5760
/// Verifies that the given macro_call actually matches the given name
@@ -90,19 +93,10 @@ fn needs_parentheses_around_macro_contents(macro_contents: Vec<SyntaxElement>) -
9093
if macro_contents.len() < 2 {
9194
return false;
9295
}
93-
94-
let mut macro_contents_kind_not_in_brackets = Vec::with_capacity(macro_contents.len());
95-
96-
let mut first_bracket_in_macro = None;
9796
let mut unpaired_brackets_in_contents = Vec::new();
9897
for element in macro_contents {
9998
match element.kind() {
100-
T!['('] | T!['['] | T!['{'] => {
101-
if let None = first_bracket_in_macro {
102-
first_bracket_in_macro = Some(element.clone())
103-
}
104-
unpaired_brackets_in_contents.push(element);
105-
}
99+
T!['('] | T!['['] | T!['{'] => unpaired_brackets_in_contents.push(element),
106100
T![')'] => {
107101
if !matches!(unpaired_brackets_in_contents.pop(), Some(correct_bracket) if correct_bracket.kind() == T!['('])
108102
{
@@ -121,19 +115,15 @@ fn needs_parentheses_around_macro_contents(macro_contents: Vec<SyntaxElement>) -
121115
return true;
122116
}
123117
}
124-
other_kind => {
125-
if unpaired_brackets_in_contents.is_empty() {
126-
macro_contents_kind_not_in_brackets.push(other_kind);
118+
symbol_kind => {
119+
let symbol_not_in_bracket = unpaired_brackets_in_contents.is_empty();
120+
if symbol_not_in_bracket && symbol_kind.is_punct() {
121+
return true;
127122
}
128123
}
129124
}
130125
}
131-
132126
!unpaired_brackets_in_contents.is_empty()
133-
|| matches!(first_bracket_in_macro, Some(bracket) if bracket.kind() != T!['('])
134-
|| macro_contents_kind_not_in_brackets
135-
.into_iter()
136-
.any(|macro_contents_kind| macro_contents_kind.is_punct())
137127
}
138128

139129
#[cfg(test)]
@@ -244,6 +234,7 @@ fn main() {
244234
);
245235

246236
check_assist(remove_dbg, r#"let res = <|>dbg!(2 + 2) * 5"#, r#"let res = (2 + 2) * 5"#);
237+
check_assist(remove_dbg, r#"let res = <|>dbg![2 + 2] * 5"#, r#"let res = (2 + 2) * 5"#);
247238
}
248239

249240
#[test]

0 commit comments

Comments
 (0)