Skip to content

Commit 353816c

Browse files
authored
Merge pull request #2730 from topecongiro/issue-2704
Disallow combining a method call with prefix or suffix
2 parents 9f00199 + 8cb2b8e commit 353816c

File tree

5 files changed

+107
-7
lines changed

5 files changed

+107
-7
lines changed

src/expr.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,3 +2173,15 @@ impl ToExpr for ast::GenericParam {
21732173
false
21742174
}
21752175
}
2176+
2177+
pub fn is_method_call(expr: &ast::Expr) -> bool {
2178+
match expr.node {
2179+
ast::ExprKind::MethodCall(..) => true,
2180+
ast::ExprKind::AddrOf(_, ref expr)
2181+
| ast::ExprKind::Box(ref expr)
2182+
| ast::ExprKind::Cast(ref expr, _)
2183+
| ast::ExprKind::Try(ref expr)
2184+
| ast::ExprKind::Unary(_, ref expr) => is_method_call(expr),
2185+
_ => false,
2186+
}
2187+
}

src/items.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,9 +1380,9 @@ fn format_tuple_struct(
13801380
// We need to put the where clause on a new line, but we didn't
13811381
// know that earlier, so the where clause will not be indented properly.
13821382
result.push('\n');
1383-
result
1384-
.push_str(&(offset.block_only() + (context.config.tab_spaces() - 1))
1385-
.to_string(context.config));
1383+
result.push_str(
1384+
&(offset.block_only() + (context.config.tab_spaces() - 1)).to_string(context.config),
1385+
);
13861386
}
13871387
result.push_str(&where_clause_str);
13881388

src/overflow.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use syntax::parse::token::DelimToken;
1818

1919
use closures;
2020
use codemap::SpanUtils;
21-
use expr::{is_every_expr_simple, is_nested_call, maybe_get_args_offset, ToExpr};
21+
use expr::{is_every_expr_simple, is_method_call, is_nested_call, maybe_get_args_offset, ToExpr};
2222
use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator};
2323
use rewrite::{Rewrite, RewriteContext};
2424
use shape::Shape;
@@ -231,8 +231,8 @@ impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> {
231231
let placeholder = if overflow_last {
232232
let old_value = *self.context.force_one_line_chain.borrow();
233233
if !combine_arg_with_callee {
234-
if let Some(expr) = self.last_item().and_then(|item| item.to_expr()) {
235-
if let ast::ExprKind::MethodCall(..) = expr.node {
234+
if let Some(ref expr) = self.last_item().and_then(|item| item.to_expr()) {
235+
if is_method_call(expr) {
236236
self.context.force_one_line_chain.replace(true);
237237
}
238238
}

tests/source/expr.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,41 @@ fn foo() {
398398
let my_var =
399399
Mutex::new(RpcClientType::connect(server_iddd).chain_err(|| "Unable to create RPC client")?);
400400
}
401+
402+
// #2704
403+
// Method call with prefix and suffix.
404+
fn issue2704() {
405+
// We should not combine the callee with a multi-lined method call.
406+
let requires = requires.set(&requires0
407+
.concat(&requires1)
408+
.concat(&requires2)
409+
.distinct_total());
410+
let requires = requires.set(box requires0
411+
.concat(&requires1)
412+
.concat(&requires2)
413+
.distinct_total());
414+
let requires = requires.set(requires0
415+
.concat(&requires1)
416+
.concat(&requires2)
417+
.distinct_total() as u32);
418+
let requires = requires.set(requires0
419+
.concat(&requires1)
420+
.concat(&requires2)
421+
.distinct_total()?);
422+
let requires = requires.set(!requires0
423+
.concat(&requires1)
424+
.concat(&requires2)
425+
.distinct_total());
426+
// We should combine a small callee with an argument.
427+
bar(vec![22]
428+
.into_iter()
429+
.map(|x| x * 2)
430+
.filter(|_| true)
431+
.collect());
432+
// But we should not combine a long callee with an argument.
433+
barrrr(vec![22]
434+
.into_iter()
435+
.map(|x| x * 2)
436+
.filter(|_| true)
437+
.collect());
438+
}

tests/target/expr.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,56 @@ fn dots() {
424424
// A function call with a large single argument.
425425
fn foo() {
426426
let my_var = Mutex::new(
427-
RpcClientType::connect(server_iddd).chain_err(|| "Unable to create RPC client")?
427+
RpcClientType::connect(server_iddd).chain_err(|| "Unable to create RPC client")?,
428+
);
429+
}
430+
431+
// #2704
432+
// Method call with prefix and suffix.
433+
fn issue2704() {
434+
// We should not combine the callee with a multi-lined method call.
435+
let requires = requires.set(
436+
&requires0
437+
.concat(&requires1)
438+
.concat(&requires2)
439+
.distinct_total(),
440+
);
441+
let requires = requires.set(
442+
box requires0
443+
.concat(&requires1)
444+
.concat(&requires2)
445+
.distinct_total(),
446+
);
447+
let requires = requires.set(
448+
requires0
449+
.concat(&requires1)
450+
.concat(&requires2)
451+
.distinct_total() as u32,
452+
);
453+
let requires = requires.set(
454+
requires0
455+
.concat(&requires1)
456+
.concat(&requires2)
457+
.distinct_total()?,
458+
);
459+
let requires = requires.set(
460+
!requires0
461+
.concat(&requires1)
462+
.concat(&requires2)
463+
.distinct_total(),
464+
);
465+
// We should combine a small callee with an argument.
466+
bar(vec![22]
467+
.into_iter()
468+
.map(|x| x * 2)
469+
.filter(|_| true)
470+
.collect());
471+
// But we should not combine a long callee with an argument.
472+
barrrr(
473+
vec![22]
474+
.into_iter()
475+
.map(|x| x * 2)
476+
.filter(|_| true)
477+
.collect(),
428478
);
429479
}

0 commit comments

Comments
 (0)