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 <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2020-10-25 14:50:48 +00:00 committed by GitHub
commit eae54b5f72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -93,8 +93,9 @@ fn needs_parentheses_around_macro_contents(macro_contents: Vec<SyntaxElement>) -
if macro_contents.len() < 2 {
return false;
}
let mut macro_contents = macro_contents.into_iter().peekable();
let mut unpaired_brackets_in_contents = Vec::new();
for element in macro_contents {
while let Some(element) = macro_contents.next() {
match element.kind() {
T!['('] | T!['['] | T!['{'] => unpaired_brackets_in_contents.push(element),
T![')'] => {
@ -118,8 +119,14 @@ fn needs_parentheses_around_macro_contents(macro_contents: Vec<SyntaxElement>) -
symbol_kind => {
let symbol_not_in_bracket = unpaired_brackets_in_contents.is_empty();
if symbol_not_in_bracket
&& symbol_kind != SyntaxKind::COLON
&& symbol_kind.is_punct()
&& symbol_kind != SyntaxKind::COLON // paths
&& (symbol_kind != SyntaxKind::DOT // field/method access
|| macro_contents // range expressions consist of two SyntaxKind::Dot in macro invocations
.peek()
.map(|element| element.kind() == SyntaxKind::DOT)
.unwrap_or(false))
&& symbol_kind != SyntaxKind::QUESTION // try operator
&& (symbol_kind.is_punct() || symbol_kind == SyntaxKind::AS_KW)
{
return true;
}
@ -242,6 +249,25 @@ fn main() {
check_assist(remove_dbg, r#"let res = <|>dbg![2 + 2] * 5"#, r#"let res = (2 + 2) * 5"#);
}
#[test]
fn test_remove_dbg_method_chaining() {
check_assist(
remove_dbg,
r#"let res = <|>dbg!(foo().bar()).baz();"#,
r#"let res = foo().bar().baz();"#,
);
check_assist(
remove_dbg,
r#"let res = <|>dbg!(foo.bar()).baz();"#,
r#"let res = foo.bar().baz();"#,
);
}
#[test]
fn test_remove_dbg_field_chaining() {
check_assist(remove_dbg, r#"let res = <|>dbg!(foo.bar).baz;"#, r#"let res = foo.bar.baz;"#);
}
#[test]
fn test_remove_dbg_from_inside_fn() {
check_assist_target(
@ -280,4 +306,59 @@ fn main() {
}"#,
);
}
#[test]
fn test_remove_dbg_try_expr() {
check_assist(
remove_dbg,
r#"let res = <|>dbg!(result?).foo();"#,
r#"let res = result?.foo();"#,
);
}
#[test]
fn test_remove_dbg_await_expr() {
check_assist(
remove_dbg,
r#"let res = <|>dbg!(fut.await).foo();"#,
r#"let res = fut.await.foo();"#,
);
}
#[test]
fn test_remove_dbg_as_cast() {
check_assist(
remove_dbg,
r#"let res = <|>dbg!(3 as usize).foo();"#,
r#"let res = (3 as usize).foo();"#,
);
}
#[test]
fn test_remove_dbg_index_expr() {
check_assist(
remove_dbg,
r#"let res = <|>dbg!(array[3]).foo();"#,
r#"let res = array[3].foo();"#,
);
check_assist(
remove_dbg,
r#"let res = <|>dbg!(tuple.3).foo();"#,
r#"let res = tuple.3.foo();"#,
);
}
#[test]
fn test_remove_dbg_range_expr() {
check_assist(
remove_dbg,
r#"let res = <|>dbg!(foo..bar).foo();"#,
r#"let res = (foo..bar).foo();"#,
);
check_assist(
remove_dbg,
r#"let res = <|>dbg!(foo..=bar).foo();"#,
r#"let res = (foo..=bar).foo();"#,
);
}
}