Don't duplicate parens in patterns

This commit is contained in:
Aleksey Kladov 2020-07-14 13:51:43 +02:00
parent f823386db8
commit f7f4ea633b
2 changed files with 37 additions and 1 deletions

View file

@ -63,6 +63,8 @@ pub(crate) struct CompletionContext<'a> {
pub(super) dot_receiver_is_ambiguous_float_literal: bool,
/// If this is a call (method or function) in particular, i.e. the () are already there.
pub(super) is_call: bool,
/// Like `is_call`, but for tuple patterns.
pub(super) is_pattern_call: bool,
/// If this is a macro call, i.e. the () are already there.
pub(super) is_macro_call: bool,
pub(super) is_path_type: bool,
@ -136,6 +138,7 @@ impl<'a> CompletionContext<'a> {
is_new_item: false,
dot_receiver: None,
is_call: false,
is_pattern_call: false,
is_macro_call: false,
is_path_type: false,
has_type_args: false,
@ -370,6 +373,8 @@ impl<'a> CompletionContext<'a> {
.and_then(|it| it.syntax().parent().and_then(ast::CallExpr::cast))
.is_some();
self.is_macro_call = path.syntax().parent().and_then(ast::MacroCall::cast).is_some();
self.is_pattern_call =
path.syntax().parent().and_then(ast::TupleStructPat::cast).is_some();
self.is_path_type = path.syntax().parent().and_then(ast::PathType::cast).is_some();
self.has_type_args = segment.type_arg_list().is_some();

View file

@ -384,10 +384,17 @@ impl Builder {
if !ctx.config.add_call_parenthesis {
return self;
}
if ctx.use_item_syntax.is_some() || ctx.is_call {
if ctx.use_item_syntax.is_some() {
mark::hit!(no_parens_in_use_item);
return self;
}
if ctx.is_pattern_call {
mark::hit!(dont_duplicate_pattern_parens);
return self;
}
if ctx.is_call {
return self;
}
// Don't add parentheses if the expected type is some function reference.
if let Some(ty) = &ctx.expected_type {
@ -907,6 +914,30 @@ fn main(value: Option<i32>) {
);
}
#[test]
fn dont_duplicate_pattern_parens() {
mark::check!(dont_duplicate_pattern_parens);
check_edit(
"Var",
r#"
enum E { Var(i32) }
fn main() {
match E::Var(92) {
E::<|>(92) => (),
}
}
"#,
r#"
enum E { Var(i32) }
fn main() {
match E::Var(92) {
E::Var(92) => (),
}
}
"#,
);
}
#[test]
fn no_call_parens_if_fn_ptr_needed() {
mark::check!(no_call_parens_if_fn_ptr_needed);