mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Merge pull request #1745 from topecongiro/assignment
Put rhs of assignment on the next line if it fits in a single line
This commit is contained in:
commit
cfec7ad593
@ -186,8 +186,8 @@ pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -
|
||||
let almost_total = rewrites[..last_non_try_index]
|
||||
.iter()
|
||||
.fold(0, |a, b| a + first_line_width(b)) + parent_rewrite.len();
|
||||
let one_line_len = rewrites.iter().fold(0, |a, r| a + first_line_width(r)) +
|
||||
parent_rewrite.len();
|
||||
let one_line_len =
|
||||
rewrites.iter().fold(0, |a, r| a + first_line_width(r)) + parent_rewrite.len();
|
||||
|
||||
let one_line_budget = min(shape.width, context.config.chain_one_line_max());
|
||||
let veto_single_line = if one_line_len > one_line_budget {
|
||||
|
35
src/expr.rs
35
src/expr.rs
@ -974,8 +974,8 @@ fn rewrite_cond(context: &RewriteContext, expr: &ast::Expr, shape: Shape) -> Opt
|
||||
}
|
||||
_ => {
|
||||
to_control_flow(expr, ExprType::SubExpression).and_then(|control_flow| {
|
||||
let alt_block_sep = String::from("\n") +
|
||||
&shape.indent.block_only().to_string(context.config);
|
||||
let alt_block_sep =
|
||||
String::from("\n") + &shape.indent.block_only().to_string(context.config);
|
||||
control_flow
|
||||
.rewrite_cond(context, shape, &alt_block_sep)
|
||||
.and_then(|rw| Some(rw.0))
|
||||
@ -1319,8 +1319,8 @@ impl<'a> Rewrite for ControlFlow<'a> {
|
||||
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
||||
debug!("ControlFlow::rewrite {:?} {:?}", self, shape);
|
||||
|
||||
let alt_block_sep = String::from("\n") +
|
||||
&shape.indent.block_only().to_string(context.config);
|
||||
let alt_block_sep =
|
||||
String::from("\n") + &shape.indent.block_only().to_string(context.config);
|
||||
let (cond_str, used_width) = try_opt!(self.rewrite_cond(context, shape, &alt_block_sep));
|
||||
// If `used_width` is 0, it indicates that whole control flow is written in a single line.
|
||||
if used_width == 0 {
|
||||
@ -1743,8 +1743,8 @@ impl Rewrite for ast::Arm {
|
||||
extend &= context.use_block_indent();
|
||||
|
||||
let comma = arm_comma(&context.config, body);
|
||||
let alt_block_sep = String::from("\n") +
|
||||
&shape.indent.block_only().to_string(context.config);
|
||||
let alt_block_sep =
|
||||
String::from("\n") + &shape.indent.block_only().to_string(context.config);
|
||||
|
||||
let pat_width = extra_offset(&pats_str, shape);
|
||||
// Let's try and get the arm body on the same line as the condition.
|
||||
@ -2963,26 +2963,21 @@ pub fn rewrite_assign_rhs<S: Into<String>>(
|
||||
let orig_shape = try_opt!(shape.offset_left(last_line_width + 1));
|
||||
let rhs = ex.rewrite(context, orig_shape);
|
||||
|
||||
fn count_line_breaks(src: &str) -> usize {
|
||||
src.chars().filter(|&x| x == '\n').count()
|
||||
}
|
||||
|
||||
match rhs {
|
||||
Some(ref new_str) if count_line_breaks(new_str) < 2 => {
|
||||
Some(ref new_str) if !new_str.contains('\n') => {
|
||||
result.push(' ');
|
||||
result.push_str(new_str);
|
||||
}
|
||||
_ => {
|
||||
// Expression did not fit on the same line as the identifier or is
|
||||
// at least three lines big. Try splitting the line and see
|
||||
// if that works better.
|
||||
// Expression did not fit on the same line as the identifier.
|
||||
// Try splitting the line and see if that works better.
|
||||
let new_shape = try_opt!(shape.block_left(context.config.tab_spaces()));
|
||||
let new_rhs = ex.rewrite(context, new_shape);
|
||||
|
||||
// FIXME: DRY!
|
||||
match (rhs, new_rhs) {
|
||||
(Some(ref orig_rhs), Some(ref replacement_rhs))
|
||||
if count_line_breaks(orig_rhs) > count_line_breaks(replacement_rhs) + 1 => {
|
||||
if prefer_next_line(orig_rhs, replacement_rhs) => {
|
||||
result.push_str(&format!("\n{}", new_shape.indent.to_string(context.config)));
|
||||
result.push_str(replacement_rhs);
|
||||
}
|
||||
@ -3002,6 +2997,16 @@ pub fn rewrite_assign_rhs<S: Into<String>>(
|
||||
Some(result)
|
||||
}
|
||||
|
||||
fn prefer_next_line(orig_rhs: &str, next_line_rhs: &str) -> bool {
|
||||
|
||||
fn count_line_breaks(src: &str) -> usize {
|
||||
src.chars().filter(|&x| x == '\n').count()
|
||||
}
|
||||
|
||||
!next_line_rhs.contains('\n') ||
|
||||
count_line_breaks(orig_rhs) > count_line_breaks(next_line_rhs) + 1
|
||||
}
|
||||
|
||||
fn rewrite_expr_addrof(
|
||||
context: &RewriteContext,
|
||||
mutability: ast::Mutability,
|
||||
|
37
src/items.rs
37
src/items.rs
@ -1503,22 +1503,23 @@ pub fn rewrite_static(
|
||||
span: Span,
|
||||
context: &RewriteContext,
|
||||
) -> Option<String> {
|
||||
let type_annotation_spacing = type_annotation_spacing(context.config);
|
||||
let colon = colon_spaces(
|
||||
context.config.space_before_type_annotation(),
|
||||
context.config.space_after_type_annotation_colon(),
|
||||
);
|
||||
let prefix = format!(
|
||||
"{}{} {}{}{}:{}",
|
||||
"{}{} {}{}{}",
|
||||
format_visibility(vis),
|
||||
prefix,
|
||||
format_mutability(mutability),
|
||||
ident,
|
||||
type_annotation_spacing.0,
|
||||
type_annotation_spacing.1
|
||||
colon,
|
||||
);
|
||||
// 2 = " =".len()
|
||||
let ty_str = try_opt!(ty.rewrite(
|
||||
context,
|
||||
Shape::legacy(
|
||||
context.config.max_width() - offset.block_indent - prefix.len() - 2,
|
||||
offset.block_only(),
|
||||
try_opt!(
|
||||
Shape::indented(offset.block_only(), context.config).offset_left(prefix.len() + 2)
|
||||
),
|
||||
));
|
||||
|
||||
@ -1532,21 +1533,11 @@ pub fn rewrite_static(
|
||||
expr,
|
||||
Shape::legacy(remaining_width, offset.block_only()),
|
||||
).and_then(|res| {
|
||||
recover_comment_removed(
|
||||
res,
|
||||
span,
|
||||
context,
|
||||
Shape {
|
||||
width: context.config.max_width(),
|
||||
indent: offset,
|
||||
offset: offset.alignment,
|
||||
},
|
||||
)
|
||||
recover_comment_removed(res, span, context, Shape::indented(offset, context.config))
|
||||
})
|
||||
.map(|s| if s.ends_with(';') { s } else { s + ";" })
|
||||
} else {
|
||||
let lhs = format!("{}{};", prefix, ty_str);
|
||||
Some(lhs)
|
||||
Some(format!("{}{};", prefix, ty_str))
|
||||
}
|
||||
}
|
||||
|
||||
@ -1933,8 +1924,8 @@ fn rewrite_fn_base(
|
||||
generics_str.contains('\n'),
|
||||
));
|
||||
|
||||
let multi_line_arg_str = arg_str.contains('\n') ||
|
||||
arg_str.chars().last().map_or(false, |c| c == ',');
|
||||
let multi_line_arg_str =
|
||||
arg_str.contains('\n') || arg_str.chars().last().map_or(false, |c| c == ',');
|
||||
|
||||
let put_args_in_block = match context.config.fn_args_layout() {
|
||||
IndentStyle::Block => multi_line_arg_str || generics_str.contains('\n'),
|
||||
@ -2320,8 +2311,8 @@ fn compute_budgets_for_args(
|
||||
|
||||
if one_line_budget > 0 {
|
||||
// 4 = "() {".len()
|
||||
let multi_line_overhead = indent.width() + result.len() +
|
||||
if newline_brace { 2 } else { 4 };
|
||||
let multi_line_overhead =
|
||||
indent.width() + result.len() + if newline_brace { 2 } else { 4 };
|
||||
let multi_line_budget =
|
||||
try_opt!(context.config.max_width().checked_sub(multi_line_overhead));
|
||||
|
||||
|
@ -331,8 +331,8 @@ where
|
||||
.span_to_snippet(mk_sp(self.prev_span_end, (self.get_lo)(&item)))
|
||||
.unwrap();
|
||||
let trimmed_pre_snippet = pre_snippet.trim();
|
||||
let has_pre_comment = trimmed_pre_snippet.contains("//") ||
|
||||
trimmed_pre_snippet.contains("/*");
|
||||
let has_pre_comment =
|
||||
trimmed_pre_snippet.contains("//") || trimmed_pre_snippet.contains("/*");
|
||||
let pre_comment = if has_pre_comment {
|
||||
Some(trimmed_pre_snippet.to_owned())
|
||||
} else {
|
||||
|
@ -47,8 +47,8 @@ fn list_submodules<'a>(
|
||||
for item in &module.items {
|
||||
if let ast::ItemKind::Mod(ref sub_mod) = item.node {
|
||||
if !utils::contains_skip(&item.attrs) {
|
||||
let is_internal = codemap.span_to_filename(item.span) ==
|
||||
codemap.span_to_filename(sub_mod.inner);
|
||||
let is_internal =
|
||||
codemap.span_to_filename(item.span) == codemap.span_to_filename(sub_mod.inner);
|
||||
let dir_path = if is_internal {
|
||||
search_dir.join(&item.ident.to_string())
|
||||
} else {
|
||||
|
@ -16,3 +16,6 @@ pub const test: &Type = &val;
|
||||
impl Color {
|
||||
pub const WHITE: u32 = 10;
|
||||
}
|
||||
|
||||
// #1391
|
||||
pub const XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX: NTSTATUS = 0 as usize;
|
||||
|
@ -147,8 +147,8 @@ fn self_tests() {
|
||||
fn stdin_formatting_smoke_test() {
|
||||
let input = Input::Text("fn main () {}".to_owned());
|
||||
let config = Config::default();
|
||||
let (error_summary, file_map, _report) = format_input::<io::Stdout>(input, &config, None)
|
||||
.unwrap();
|
||||
let (error_summary, file_map, _report) =
|
||||
format_input::<io::Stdout>(input, &config, None).unwrap();
|
||||
assert!(error_summary.has_no_errors());
|
||||
for &(ref file_name, ref text) in &file_map {
|
||||
if file_name == "stdin" {
|
||||
@ -164,8 +164,8 @@ fn format_lines_errors_are_reported() {
|
||||
let long_identifier = String::from_utf8(vec![b'a'; 239]).unwrap();
|
||||
let input = Input::Text(format!("fn {}() {{}}", long_identifier));
|
||||
let config = Config::default();
|
||||
let (error_summary, _file_map, _report) = format_input::<io::Stdout>(input, &config, None)
|
||||
.unwrap();
|
||||
let (error_summary, _file_map, _report) =
|
||||
format_input::<io::Stdout>(input, &config, None).unwrap();
|
||||
assert!(error_summary.has_formatting_errors());
|
||||
}
|
||||
|
||||
@ -242,8 +242,8 @@ fn read_config(filename: &str) -> Config {
|
||||
|
||||
fn format_file<P: Into<PathBuf>>(filename: P, config: &Config) -> (FileMap, FormatReport) {
|
||||
let input = Input::File(filename.into());
|
||||
let (_error_summary, file_map, report) = format_input::<io::Stdout>(input, &config, None)
|
||||
.unwrap();
|
||||
let (_error_summary, file_map, report) =
|
||||
format_input::<io::Stdout>(input, &config, None).unwrap();
|
||||
return (file_map, report);
|
||||
}
|
||||
|
||||
|
@ -7,11 +7,11 @@ fn foo() -> bool {
|
||||
let referenced = &5;
|
||||
|
||||
let very_long_variable_name = (a + first + simple + test);
|
||||
let very_long_variable_name = (a + first + simple + test + AAAAAAAAAAAAA +
|
||||
BBBBBBBBBBBBBBBBB + b + c);
|
||||
let very_long_variable_name =
|
||||
(a + first + simple + test + AAAAAAAAAAAAA + BBBBBBBBBBBBBBBBB + b + c);
|
||||
|
||||
let is_internalxxxx = self.codemap.span_to_filename(s) ==
|
||||
self.codemap.span_to_filename(m.inner);
|
||||
let is_internalxxxx =
|
||||
self.codemap.span_to_filename(s) == self.codemap.span_to_filename(m.inner);
|
||||
|
||||
let some_val = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa * bbbb /
|
||||
(bbbbbb - function_call(x, *very_long_pointer, y)) + 1000;
|
||||
@ -84,8 +84,8 @@ fn foo() -> bool {
|
||||
}
|
||||
|
||||
fn bar() {
|
||||
let range = (111111111 + 333333333333333333 + 1111 + 400000000000000000)..
|
||||
(2222 + 2333333333333333);
|
||||
let range =
|
||||
(111111111 + 333333333333333333 + 1111 + 400000000000000000)..(2222 + 2333333333333333);
|
||||
|
||||
let another_range = 5..some_func(a, b /* comment */);
|
||||
|
||||
@ -274,8 +274,8 @@ fn casts() {
|
||||
}
|
||||
|
||||
let some_trait_xxx = xxxxxxxxxxx + xxxxxxxxxxxxx as SomeTraitXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX;
|
||||
let slightly_longer_trait = yyyyyyyyy +
|
||||
yyyyyyyyyyy as SomeTraitYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY;
|
||||
let slightly_longer_trait =
|
||||
yyyyyyyyy + yyyyyyyyyyy as SomeTraitYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY;
|
||||
}
|
||||
|
||||
fn indices() {
|
||||
@ -311,8 +311,8 @@ fn issue767() {
|
||||
|
||||
fn ranges() {
|
||||
let x = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
|
||||
let y = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
|
||||
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
|
||||
let y =
|
||||
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
|
||||
let z = ...x;
|
||||
|
||||
// #1766
|
||||
|
@ -1,4 +1,4 @@
|
||||
fn f() {
|
||||
block_flow.base.stacking_relative_position_of_display_port = self.base
|
||||
.stacking_relative_position_of_display_port;
|
||||
block_flow.base.stacking_relative_position_of_display_port =
|
||||
self.base.stacking_relative_position_of_display_port;
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
const FILE_GENERIC_READ: DWORD = STANDARD_RIGHTS_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES |
|
||||
FILE_READ_EA | SYNCHRONIZE;
|
||||
const FILE_GENERIC_READ: DWORD =
|
||||
STANDARD_RIGHTS_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE;
|
||||
|
||||
static boolnames: &'static [&'static str] = &[
|
||||
"bw",
|
||||
@ -58,3 +58,7 @@ pub const test: &Type = &val;
|
||||
impl Color {
|
||||
pub const WHITE: u32 = 10;
|
||||
}
|
||||
|
||||
// #1391
|
||||
pub const XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX: NTSTATUS =
|
||||
0 as usize;
|
||||
|
@ -1,9 +1,9 @@
|
||||
fn main() {
|
||||
let xxxxxxxxxxx = yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy:
|
||||
SomeTrait<AA, BB, CC>;
|
||||
let xxxxxxxxxxx =
|
||||
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy: SomeTrait<AA, BB, CC>;
|
||||
|
||||
let xxxxxxxxxxxxxxx = yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy:
|
||||
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA;
|
||||
let xxxxxxxxxxxxxxx =
|
||||
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA;
|
||||
|
||||
let z = funk(yyyyyyyyyyyyyyy, zzzzzzzzzzzzzzzz, wwwwww):
|
||||
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA;
|
||||
|
Loading…
Reference in New Issue
Block a user