format!("Invalid write-mode: {}", write_mode),
));
}
- } else {
+ } else if !matches.opt_present("no-warn-write-mode") {
println!(
"Warning: the default write-mode for Rustfmt will soon change to overwrite \
- this will not leave backups of changed files."
opts.optflag("h", "help", "show this message");
opts.optflag("V", "version", "show version information");
opts.optflag("v", "verbose", "print verbose output");
+ // Suppress warning. Remove this option after the default write mode changed to overwrite.
+ opts.optflag(
+ "w",
+ "no-warn-write-mode",
+ "inhibit warning about write-mode change",
+ );
opts.optopt(
"",
"write-mode",
// The body of the closure is big enough to be block indented, that
// means we must re-format.
- let block_shape = shape.block().with_max_width(context.config);
+ let block_shape = shape.block();
let block_str = try_opt!(block.rewrite(&context, block_shape));
Some(format!("{} {}", prefix, block_str))
}
context.use_block_indent() ||
context.config.fn_call_style() == IndentStyle::Visual && args_len > 1
}
+ ast::ExprKind::Array(..) |
ast::ExprKind::Call(..) |
- ast::ExprKind::MethodCall(..) |
ast::ExprKind::Mac(..) |
+ ast::ExprKind::MethodCall(..) |
ast::ExprKind::Struct(..) |
ast::ExprKind::Tup(..) => context.use_block_indent() && args_len == 1,
ast::ExprKind::AddrOf(_, ref expr) |
};
let offset = expr_str.len() + lbr.len();
- if let Some(index_shape) = shape.visual_indent(offset).sub_width(offset + rbr.len()) {
- if let Some(index_str) = index.rewrite(context, index_shape) {
+ let orig_index_rw = shape
+ .visual_indent(offset)
+ .sub_width(offset + rbr.len())
+ .and_then(|index_shape| index.rewrite(context, index_shape));
+
+ // Return if everything fits in a single line.
+ match orig_index_rw {
+ Some(ref index_str) if !index_str.contains('\n') => {
return Some(format!("{}{}{}{}", expr_str, lbr, index_str, rbr));
}
+ _ => (),
}
+ // Try putting index on the next line and see if it fits in a single line.
let indent = shape.indent.block_indent(&context.config);
- let indent = indent.to_string(&context.config);
- // FIXME this is not right, since we don't take into account that shape.width
- // might be reduced from max_width by something on the right.
- let budget = try_opt!(
- context
- .config
- .max_width()
- .checked_sub(indent.len() + lbr.len() + rbr.len())
- );
- let index_str = try_opt!(index.rewrite(context, Shape::legacy(budget, shape.indent)));
- Some(format!(
- "{}\n{}{}{}{}",
- expr_str,
- indent,
- lbr,
- index_str,
- rbr
- ))
+ let rhs_overhead = context
+ .config
+ .max_width()
+ .checked_sub(shape.used_width() + shape.width)
+ .unwrap_or(0);
+ let index_shape = try_opt!(Shape::indented(indent, context.config).offset_left(lbr.len()));
+ let index_shape = try_opt!(index_shape.sub_width(rbr.len() + rhs_overhead));
+ let new_index_rw = index.rewrite(context, index_shape);
+ match (orig_index_rw, new_index_rw) {
+ (_, Some(ref new_index_str)) if !new_index_str.contains('\n') => {
+ Some(format!(
+ "{}\n{}{}{}{}",
+ expr_str,
+ indent.to_string(&context.config),
+ lbr,
+ new_index_str,
+ rbr
+ ))
+ }
+ (None, Some(ref new_index_str)) => {
+ Some(format!(
+ "{}\n{}{}{}{}",
+ expr_str,
+ indent.to_string(&context.config),
+ lbr,
+ new_index_str,
+ rbr
+ ))
+ }
+ (Some(ref index_str), _) => Some(format!("{}{}{}{}", expr_str, lbr, index_str, rbr)),
+ _ => None,
+ }
}
fn struct_lit_can_be_aligned(fields: &[ast::Field], base: &Option<&ast::Expr>) -> bool {
1,
];
- let a = WeightedChoice::new(
- &mut [
- Weighted { weight: x, item: 0 },
- Weighted { weight: 1, item: 1 },
- Weighted { weight: x, item: 2 },
- Weighted { weight: 1, item: 3 },
- ],
- );
+ let a = WeightedChoice::new(&mut [
+ Weighted { weight: x, item: 0 },
+ Weighted { weight: 1, item: 1 },
+ Weighted { weight: x, item: 2 },
+ Weighted { weight: 1, item: 3 },
+ ]);
let z = [
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx,
fn issue775() {
if indent {
- let a = mk_object(
- &[
- ("a".to_string(), Boolean(true)),
- (
- "b".to_string(),
- Array(vec![
- mk_object(
- &[("c".to_string(), String("\x0c\r".to_string()))],
- ),
- mk_object(&[("d".to_string(), String("".to_string()))]),
- ]),
- ),
- ],
- );
+ let a = mk_object(&[
+ ("a".to_string(), Boolean(true)),
+ (
+ "b".to_string(),
+ Array(vec![
+ mk_object(
+ &[("c".to_string(), String("\x0c\r".to_string()))],
+ ),
+ mk_object(&[("d".to_string(), String("".to_string()))]),
+ ]),
+ ),
+ ]);
}
}