about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorCatherine <114838443+Centri3@users.noreply.github.com>2023-07-18 09:41:56 -0500
committerYacin Tmimi <yacintmimi@gmail.com>2023-08-01 04:36:16 -0400
commita72613be50f8afa39c2d46f732252463dc9cc14f (patch)
tree543b3b1422f765dd6ac57698ab3f848fb30930b4 /src
parentcdfa2f86b729b0bbba8661bca075a5974cec83bf (diff)
downloadrust-a72613be50f8afa39c2d46f732252463dc9cc14f.tar.gz
rust-a72613be50f8afa39c2d46f732252463dc9cc14f.zip
Add parenthesis around closure method call
Diffstat (limited to 'src')
-rw-r--r--src/chains.rs25
1 files changed, 20 insertions, 5 deletions
diff --git a/src/chains.rs b/src/chains.rs
index bf09d817ed1..d4876389c86 100644
--- a/src/chains.rs
+++ b/src/chains.rs
@@ -253,11 +253,7 @@ impl ChainItemKind {
                 return (
                     ChainItemKind::Parent {
                         expr: expr.clone(),
-                        parens: is_method_call_receiver
-                            && matches!(
-                                &expr.kind,
-                                ast::ExprKind::Lit(lit) if crate::expr::lit_ends_in_dot(lit)
-                            ),
+                        parens: is_method_call_receiver && should_add_parens(expr),
                     },
                     expr.span,
                 );
@@ -982,3 +978,22 @@ fn trim_tries(s: &str) -> String {
     }
     result
 }
+
+/// Whether a method call's receiver needs parenthesis, like
+/// ```rust,ignore
+/// || .. .method();
+/// || 1.. .method();
+/// 1. .method();
+/// ```
+/// Which all need parenthesis or a space before `.method()`.
+fn should_add_parens(expr: &ast::Expr) -> bool {
+    match expr.kind {
+        ast::ExprKind::Lit(ref lit) => crate::expr::lit_ends_in_dot(lit),
+        ast::ExprKind::Closure(ref cl) => match cl.body.kind {
+            ast::ExprKind::Range(_, _, ast::RangeLimits::HalfOpen) => true,
+            ast::ExprKind::Lit(ref lit) => crate::expr::lit_ends_in_dot(lit),
+            _ => false,
+        },
+        _ => false,
+    }
+}