Skip to content

Commit 453913f

Browse files
committed
Merge branch 'master' into win-test-crlf
2 parents 206f9d7 + 802df67 commit 453913f

File tree

15 files changed

+460
-680
lines changed

15 files changed

+460
-680
lines changed

Contributing.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,13 @@ places) is for the caller to shuffle around some of its other items to make
184184
more width, then call the function again with more space.
185185

186186
Since it is common for callers to bail out when a callee fails, we often use a
187-
`try_opt!` macro to make this pattern more succinct.
187+
`?` operator to make this pattern more succinct.
188188

189189
One way we might find out that we don't have enough space is when computing how much
190190
space we have. Something like `available_space = budget - overhead`. Since
191191
widths are unsized integers, this would cause underflow. Therefore we use
192-
checked subtraction: `available_space = try_opt!(budget.checked_sub(overhead))`.
193-
`checked_sub` returns an `Option`, and if we would underflow `try_opt!` returns
192+
checked subtraction: `available_space = budget.checked_sub(overhead)?`.
193+
`checked_sub` returns an `Option`, and if we would underflow `?` returns
194194
`None`, otherwise we proceed with the computed space.
195195

196196
Much syntax in Rust is lists: lists of arguments, lists of fields, lists of

src/chains.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,9 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
110110
} else {
111111
shape
112112
};
113-
let parent_rewrite = try_opt!(
114-
parent
115-
.rewrite(context, parent_shape)
116-
.map(|parent_rw| parent_rw + &repeat_try(prefix_try_num))
117-
);
113+
let parent_rewrite = parent
114+
.rewrite(context, parent_shape)
115+
.map(|parent_rw| parent_rw + &repeat_try(prefix_try_num))?;
118116
let parent_rewrite_contains_newline = parent_rewrite.contains('\n');
119117
let is_small_parent = parent_rewrite.len() <= context.config.tab_spaces();
120118

@@ -146,8 +144,8 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
146144
let overhead = last_line_width(&parent_rewrite);
147145
let offset = parent_rewrite.lines().rev().next().unwrap().trim().len();
148146
match context.config.chain_indent() {
149-
IndentStyle::Visual => try_opt!(parent_shape.offset_left(overhead)),
150-
IndentStyle::Block => try_opt!(parent_shape.block().offset_left(offset)),
147+
IndentStyle::Visual => parent_shape.offset_left(overhead)?,
148+
IndentStyle::Block => parent_shape.block().offset_left(offset)?,
151149
}
152150
} else {
153151
other_child_shape
@@ -165,11 +163,9 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
165163
let last_subexpr = &subexpr_list[suffix_try_num];
166164
let subexpr_list = &subexpr_list[suffix_try_num..subexpr_num - prefix_try_num];
167165
let iter = subexpr_list.iter().skip(1).rev().zip(child_shape_iter);
168-
let mut rewrites = try_opt!(
169-
iter.map(|(e, shape)| {
170-
rewrite_chain_subexpr(e, total_span, context, shape)
171-
}).collect::<Option<Vec<_>>>()
172-
);
166+
let mut rewrites = iter.map(|(e, shape)| {
167+
rewrite_chain_subexpr(e, total_span, context, shape)
168+
}).collect::<Option<Vec<_>>>()?;
173169

174170
// Total of all items excluding the last.
175171
let extend_last_subexr = last_line_extendable(&parent_rewrite) && rewrites.is_empty();
@@ -187,7 +183,7 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
187183
&& rewrites.iter().all(|s| !s.contains('\n'))
188184
&& almost_total < one_line_budget;
189185
let rewrite_last = || rewrite_chain_subexpr(last_subexpr, total_span, context, nested_shape);
190-
let (last_subexpr_str, fits_single_line) = try_opt!(if all_in_one_line || extend_last_subexr {
186+
let (last_subexpr_str, fits_single_line) = if all_in_one_line || extend_last_subexr {
191187
parent_shape.offset_left(almost_total).map(|shape| {
192188
if let Some(rw) = rewrite_chain_subexpr(last_subexpr, total_span, context, shape) {
193189
let line_count = rw.lines().count();
@@ -207,11 +203,11 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
207203
} else {
208204
(rewrite_last(), false)
209205
}
210-
})
206+
})?
211207
} else {
212-
Some((rewrite_last(), false))
213-
});
214-
rewrites.push(try_opt!(last_subexpr_str));
208+
(rewrite_last(), false)
209+
};
210+
rewrites.push(last_subexpr_str?);
215211

216212
let connector = if fits_single_line && !parent_rewrite_contains_newline {
217213
// Yay, we can put everything on one line.
@@ -288,7 +284,7 @@ fn rewrite_try(
288284
context: &RewriteContext,
289285
shape: Shape,
290286
) -> Option<String> {
291-
let sub_expr = try_opt!(expr.rewrite(context, try_opt!(shape.sub_width(try_count))));
287+
let sub_expr = expr.rewrite(context, shape.sub_width(try_count)?)?;
292288
Some(format!("{}{}", sub_expr, repeat_try(try_count)))
293289
}
294290

@@ -472,8 +468,10 @@ fn rewrite_method_call(
472468
let (lo, type_str) = if types.is_empty() {
473469
(args[0].span.hi(), String::new())
474470
} else {
475-
let type_list: Vec<_> =
476-
try_opt!(types.iter().map(|ty| ty.rewrite(context, shape)).collect());
471+
let type_list = types
472+
.iter()
473+
.map(|ty| ty.rewrite(context, shape))
474+
.collect::<Option<Vec<_>>>()?;
477475

478476
let type_str = if context.config.spaces_within_angle_brackets() && !type_list.is_empty() {
479477
format!("::< {} >", type_list.join(", "))

src/comment.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ pub fn combine_strs_with_missing_comments(
152152
last_line_width(prev_str) + first_line_width(next_str) + first_sep.len();
153153

154154
let indent_str = shape.indent.to_string(context.config);
155-
let missing_comment = try_opt!(rewrite_missing_comment(span, shape, context));
155+
let missing_comment = rewrite_missing_comment(span, shape, context)?;
156156

157157
if missing_comment.is_empty() {
158158
if allow_extend && prev_str.len() + first_sep.len() + next_str.len() <= shape.width {
@@ -254,13 +254,7 @@ fn identify_comment(
254254
.collect::<Vec<_>>()
255255
.join("\n");
256256

257-
let first_group_str = try_opt!(rewrite_comment_inner(
258-
&first_group,
259-
block_style,
260-
style,
261-
shape,
262-
config,
263-
));
257+
let first_group_str = rewrite_comment_inner(&first_group, block_style, style, shape, config)?;
264258
if rest.is_empty() {
265259
Some(first_group_str)
266260
} else {
@@ -380,7 +374,7 @@ pub fn recover_missing_comment_in_span(
380374
context: &RewriteContext,
381375
used_width: usize,
382376
) -> Option<String> {
383-
let missing_comment = try_opt!(rewrite_missing_comment(span, shape, context));
377+
let missing_comment = rewrite_missing_comment(span, shape, context)?;
384378
if missing_comment.is_empty() {
385379
Some(String::new())
386380
} else {
@@ -641,7 +635,7 @@ where
641635
type Item = (FullCodeCharKind, T::Item);
642636

643637
fn next(&mut self) -> Option<(FullCodeCharKind, T::Item)> {
644-
let item = try_opt!(self.base.next());
638+
let item = self.base.next()?;
645639
let chr = item.get_char();
646640
let mut char_kind = FullCodeCharKind::Normal;
647641
self.status = match self.status {
@@ -749,7 +743,7 @@ impl<'a> Iterator for UngroupedCommentCodeSlices<'a> {
749743
type Item = (CodeCharKind, usize, &'a str);
750744

751745
fn next(&mut self) -> Option<Self::Item> {
752-
let (kind, (start_idx, _)) = try_opt!(self.iter.next());
746+
let (kind, (start_idx, _)) = self.iter.next()?;
753747
match kind {
754748
FullCodeCharKind::Normal | FullCodeCharKind::InString => {
755749
// Consume all the Normal code
@@ -933,14 +927,14 @@ impl<'a> Iterator for CommentReducer<'a> {
933927
type Item = char;
934928
fn next(&mut self) -> Option<Self::Item> {
935929
loop {
936-
let mut c = try_opt!(self.iter.next());
930+
let mut c = self.iter.next()?;
937931
if self.is_block && self.at_start_line {
938932
while c.is_whitespace() {
939-
c = try_opt!(self.iter.next());
933+
c = self.iter.next()?;
940934
}
941935
// Ignore leading '*'
942936
if c == '*' {
943-
c = try_opt!(self.iter.next());
937+
c = self.iter.next()?;
944938
}
945939
} else if c == '\n' {
946940
self.at_start_line = true;

0 commit comments

Comments
 (0)