Skip to content

Commit 4a4addc

Browse files
committed
Box ExprKind::{Closure,MethodCall}, and QSelf in expressions, types, and patterns.
1 parent 826fb78 commit 4a4addc

File tree

7 files changed

+49
-44
lines changed

7 files changed

+49
-44
lines changed

src/attr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,10 @@ impl Rewrite for ast::MetaItem {
290290
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
291291
Some(match self.kind {
292292
ast::MetaItemKind::Word => {
293-
rewrite_path(context, PathContext::Type, None, &self.path, shape)?
293+
rewrite_path(context, PathContext::Type, &None, &self.path, shape)?
294294
}
295295
ast::MetaItemKind::List(ref list) => {
296-
let path = rewrite_path(context, PathContext::Type, None, &self.path, shape)?;
296+
let path = rewrite_path(context, PathContext::Type, &None, &self.path, shape)?;
297297
let has_trailing_comma = crate::expr::span_ends_with_comma(context, self.span);
298298
overflow::rewrite_with_parens(
299299
context,
@@ -311,7 +311,7 @@ impl Rewrite for ast::MetaItem {
311311
)?
312312
}
313313
ast::MetaItemKind::NameValue(ref literal) => {
314-
let path = rewrite_path(context, PathContext::Type, None, &self.path, shape)?;
314+
let path = rewrite_path(context, PathContext::Type, &None, &self.path, shape)?;
315315
// 3 = ` = `
316316
let lit_shape = shape.shrink_left(path.len() + 3)?;
317317
// `rewrite_literal` returns `None` when `literal` exceeds max

src/chains.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ impl ChainItemKind {
145145

146146
fn from_ast(context: &RewriteContext<'_>, expr: &ast::Expr) -> (ChainItemKind, Span) {
147147
let (kind, span) = match expr.kind {
148-
ast::ExprKind::MethodCall(ref segment, ref receiver, ref expressions, _) => {
149-
let types = if let Some(ref generic_args) = segment.args {
148+
ast::ExprKind::MethodCall(ref call) => {
149+
let types = if let Some(ref generic_args) = call.seg.args {
150150
if let ast::GenericArgs::AngleBracketed(ref data) = **generic_args {
151151
data.args
152152
.iter()
@@ -163,8 +163,8 @@ impl ChainItemKind {
163163
} else {
164164
vec![]
165165
};
166-
let span = mk_sp(receiver.span.hi(), expr.span.hi());
167-
let kind = ChainItemKind::MethodCall(segment.clone(), types, expressions.clone());
166+
let span = mk_sp(call.receiver.span.hi(), expr.span.hi());
167+
let kind = ChainItemKind::MethodCall(call.seg.clone(), types, call.args.clone());
168168
(kind, span)
169169
}
170170
ast::ExprKind::Field(ref nested, field) => {
@@ -400,9 +400,7 @@ impl Chain {
400400
// is a try! macro, we'll convert it to shorthand when the option is set.
401401
fn pop_expr_chain(expr: &ast::Expr, context: &RewriteContext<'_>) -> Option<ast::Expr> {
402402
match expr.kind {
403-
ast::ExprKind::MethodCall(_, ref receiver, _, _) => {
404-
Some(Self::convert_try(&receiver, context))
405-
}
403+
ast::ExprKind::MethodCall(ref call) => Some(Self::convert_try(&call.receiver, context)),
406404
ast::ExprKind::Field(ref subexpr, _)
407405
| ast::ExprKind::Try(ref subexpr)
408406
| ast::ExprKind::Await(ref subexpr) => Some(Self::convert_try(subexpr, context)),

src/closures.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -326,16 +326,16 @@ pub(crate) fn rewrite_last_closure(
326326
expr: &ast::Expr,
327327
shape: Shape,
328328
) -> Option<String> {
329-
if let ast::ExprKind::Closure(
330-
ref binder,
331-
capture,
332-
ref is_async,
333-
movability,
334-
ref fn_decl,
335-
ref body,
336-
_,
337-
) = expr.kind
338-
{
329+
if let ast::ExprKind::Closure(ref closure) = expr.kind {
330+
let ast::Closure {
331+
ref binder,
332+
capture_clause,
333+
ref asyncness,
334+
movability,
335+
ref fn_decl,
336+
ref body,
337+
fn_decl_span: _,
338+
} = **closure;
339339
let body = match body.kind {
340340
ast::ExprKind::Block(ref block, _)
341341
if !is_unsafe_block(block)
@@ -347,7 +347,15 @@ pub(crate) fn rewrite_last_closure(
347347
_ => body,
348348
};
349349
let (prefix, extra_offset) = rewrite_closure_fn_decl(
350-
binder, capture, is_async, movability, fn_decl, body, expr.span, context, shape,
350+
binder,
351+
capture_clause,
352+
asyncness,
353+
movability,
354+
fn_decl,
355+
body,
356+
expr.span,
357+
context,
358+
shape,
351359
)?;
352360
// If the closure goes multi line before its body, do not overflow the closure.
353361
if prefix.contains('\n') {

src/expr.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub(crate) fn format_expr(
116116
rewrite_struct_lit(
117117
context,
118118
path,
119-
qself.as_ref(),
119+
qself,
120120
fields,
121121
rest,
122122
&expr.attrs,
@@ -169,7 +169,7 @@ pub(crate) fn format_expr(
169169
rewrite_match(context, cond, arms, shape, expr.span, &expr.attrs)
170170
}
171171
ast::ExprKind::Path(ref qself, ref path) => {
172-
rewrite_path(context, PathContext::Expr, qself.as_ref(), path, shape)
172+
rewrite_path(context, PathContext::Expr, qself, path, shape)
173173
}
174174
ast::ExprKind::Assign(ref lhs, ref rhs, _) => {
175175
rewrite_assignment(context, lhs, rhs, None, shape)
@@ -203,16 +203,16 @@ pub(crate) fn format_expr(
203203
Some("yield".to_string())
204204
}
205205
}
206-
ast::ExprKind::Closure(
207-
ref binder,
208-
capture,
209-
ref is_async,
210-
movability,
211-
ref fn_decl,
212-
ref body,
213-
_,
214-
) => closures::rewrite_closure(
215-
binder, capture, is_async, movability, fn_decl, body, expr.span, context, shape,
206+
ast::ExprKind::Closure(ref cl) => closures::rewrite_closure(
207+
&cl.binder,
208+
cl.capture_clause,
209+
&cl.asyncness,
210+
cl.movability,
211+
&cl.fn_decl,
212+
&cl.body,
213+
expr.span,
214+
context,
215+
shape,
216216
),
217217
ast::ExprKind::Try(..)
218218
| ast::ExprKind::Field(..)
@@ -1537,7 +1537,7 @@ fn struct_lit_can_be_aligned(fields: &[ast::ExprField], has_base: bool) -> bool
15371537
fn rewrite_struct_lit<'a>(
15381538
context: &RewriteContext<'_>,
15391539
path: &ast::Path,
1540-
qself: Option<&ast::QSelf>,
1540+
qself: &Option<ptr::P<ast::QSelf>>,
15411541
fields: &'a [ast::ExprField],
15421542
struct_rest: &ast::StructRest,
15431543
attrs: &[ast::Attribute],

src/patterns.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,10 @@ impl Rewrite for Pat {
227227
}
228228
PatKind::Tuple(ref items) => rewrite_tuple_pat(items, None, self.span, context, shape),
229229
PatKind::Path(ref q_self, ref path) => {
230-
rewrite_path(context, PathContext::Expr, q_self.as_ref(), path, shape)
230+
rewrite_path(context, PathContext::Expr, q_self, path, shape)
231231
}
232232
PatKind::TupleStruct(ref q_self, ref path, ref pat_vec) => {
233-
let path_str =
234-
rewrite_path(context, PathContext::Expr, q_self.as_ref(), path, shape)?;
233+
let path_str = rewrite_path(context, PathContext::Expr, q_self, path, shape)?;
235234
rewrite_tuple_pat(pat_vec, Some(path_str), self.span, context, shape)
236235
}
237236
PatKind::Lit(ref expr) => expr.rewrite(context, shape),
@@ -271,7 +270,7 @@ impl Rewrite for Pat {
271270
}
272271

273272
fn rewrite_struct_pat(
274-
qself: &Option<ast::QSelf>,
273+
qself: &Option<ptr::P<ast::QSelf>>,
275274
path: &ast::Path,
276275
fields: &[ast::PatField],
277276
ellipsis: bool,
@@ -281,7 +280,7 @@ fn rewrite_struct_pat(
281280
) -> Option<String> {
282281
// 2 = ` {`
283282
let path_shape = shape.sub_width(2)?;
284-
let path_str = rewrite_path(context, PathContext::Expr, qself.as_ref(), path, path_shape)?;
283+
let path_str = rewrite_path(context, PathContext::Expr, qself, path, path_shape)?;
285284

286285
if fields.is_empty() && !ellipsis {
287286
return Some(format!("{} {{}}", path_str));

src/types.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ pub(crate) enum PathContext {
3838
pub(crate) fn rewrite_path(
3939
context: &RewriteContext<'_>,
4040
path_context: PathContext,
41-
qself: Option<&ast::QSelf>,
41+
qself: &Option<ptr::P<ast::QSelf>>,
4242
path: &ast::Path,
4343
shape: Shape,
4444
) -> Option<String> {
45-
let skip_count = qself.map_or(0, |x| x.position);
45+
let skip_count = qself.as_ref().map_or(0, |x| x.position);
4646

4747
let mut result = if path.is_global() && qself.is_none() && path_context != PathContext::Import {
4848
"::".to_owned()
@@ -655,7 +655,7 @@ impl Rewrite for ast::PolyTraitRef {
655655

656656
impl Rewrite for ast::TraitRef {
657657
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
658-
rewrite_path(context, PathContext::Type, None, &self.path, shape)
658+
rewrite_path(context, PathContext::Type, &None, &self.path, shape)
659659
}
660660
}
661661

@@ -800,7 +800,7 @@ impl Rewrite for ast::Ty {
800800
rewrite_tuple(context, items.iter(), self.span, shape, items.len() == 1)
801801
}
802802
ast::TyKind::Path(ref q_self, ref path) => {
803-
rewrite_path(context, PathContext::Type, q_self.as_ref(), path, shape)
803+
rewrite_path(context, PathContext::Type, q_self, path, shape)
804804
}
805805
ast::TyKind::Array(ref ty, ref repeats) => rewrite_pair(
806806
&**ty,

src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,9 +479,9 @@ pub(crate) fn is_block_expr(context: &RewriteContext<'_>, expr: &ast::Expr, repr
479479
| ast::ExprKind::Binary(_, _, ref expr)
480480
| ast::ExprKind::Index(_, ref expr)
481481
| ast::ExprKind::Unary(_, ref expr)
482-
| ast::ExprKind::Closure(_, _, _, _, _, ref expr, _)
483482
| ast::ExprKind::Try(ref expr)
484483
| ast::ExprKind::Yield(Some(ref expr)) => is_block_expr(context, expr, repr),
484+
ast::ExprKind::Closure(ref closure) => is_block_expr(context, &closure.body, repr),
485485
// This can only be a string lit
486486
ast::ExprKind::Lit(_) => {
487487
repr.contains('\n') && trimmed_last_line_width(repr) <= context.config.tab_spaces()

0 commit comments

Comments
 (0)