Skip to content

Commit a72613b

Browse files
Centri3ytmimi
authored andcommitted
Add parenthesis around closure method call
1 parent cdfa2f8 commit a72613b

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

src/chains.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,7 @@ impl ChainItemKind {
253253
return (
254254
ChainItemKind::Parent {
255255
expr: expr.clone(),
256-
parens: is_method_call_receiver
257-
&& matches!(
258-
&expr.kind,
259-
ast::ExprKind::Lit(lit) if crate::expr::lit_ends_in_dot(lit)
260-
),
256+
parens: is_method_call_receiver && should_add_parens(expr),
261257
},
262258
expr.span,
263259
);
@@ -982,3 +978,22 @@ fn trim_tries(s: &str) -> String {
982978
}
983979
result
984980
}
981+
982+
/// Whether a method call's receiver needs parenthesis, like
983+
/// ```rust,ignore
984+
/// || .. .method();
985+
/// || 1.. .method();
986+
/// 1. .method();
987+
/// ```
988+
/// Which all need parenthesis or a space before `.method()`.
989+
fn should_add_parens(expr: &ast::Expr) -> bool {
990+
match expr.kind {
991+
ast::ExprKind::Lit(ref lit) => crate::expr::lit_ends_in_dot(lit),
992+
ast::ExprKind::Closure(ref cl) => match cl.body.kind {
993+
ast::ExprKind::Range(_, _, ast::RangeLimits::HalfOpen) => true,
994+
ast::ExprKind::Lit(ref lit) => crate::expr::lit_ends_in_dot(lit),
995+
_ => false,
996+
},
997+
_ => false,
998+
}
999+
}

tests/source/issue-4808.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
trait Trait {
2+
fn method(&self) {}
3+
}
4+
5+
impl<F: Fn() -> T, T> Trait for F {}
6+
7+
impl Trait for f32 {}
8+
9+
fn main() {
10+
|| 10. .method();
11+
|| .. .method();
12+
|| 1.. .method();
13+
}

tests/target/issue-4808.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
trait Trait {
2+
fn method(&self) {}
3+
}
4+
5+
impl<F: Fn() -> T, T> Trait for F {}
6+
7+
impl Trait for f32 {}
8+
9+
fn main() {
10+
|| (10.).method();
11+
(|| ..).method();
12+
(|| 1..).method();
13+
}

0 commit comments

Comments
 (0)