Skip to content

Commit be00003

Browse files
authored
Merge pull request #2350 from topecongiro/issue-2324
Issue 2324
2 parents 41b14b6 + 727f7b0 commit be00003

File tree

4 files changed

+45
-15
lines changed

4 files changed

+45
-15
lines changed

src/expr.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ use shape::{Indent, Shape};
3131
use spanned::Spanned;
3232
use string::{rewrite_string, StringFormat};
3333
use types::{can_be_overflowed_type, rewrite_path, PathContext};
34-
use utils::{colon_spaces, contains_skip, extra_offset, first_line_width, inner_attributes,
35-
last_line_extendable, last_line_width, mk_sp, outer_attributes, paren_overhead,
36-
ptr_vec_to_ref_vec, semicolon_for_stmt, trimmed_last_line_width, wrap_str};
34+
use utils::{colon_spaces, contains_skip, count_newlines, extra_offset, first_line_width,
35+
inner_attributes, last_line_extendable, last_line_width, mk_sp, outer_attributes,
36+
paren_overhead, ptr_vec_to_ref_vec, semicolon_for_stmt, trimmed_last_line_width,
37+
wrap_str};
3738
use vertical::rewrite_with_alignment;
3839
use visitor::FmtVisitor;
3940

@@ -2053,6 +2054,26 @@ where
20532054
// Replace the stub with the full overflowing last argument if the rewrite
20542055
// succeeded and its first line fits with the other arguments.
20552056
match (overflow_last, tactic, placeholder) {
2057+
(true, DefinitiveListTactic::Horizontal, Some(ref overflowed)) if args.len() == 1 => {
2058+
// When we are rewriting a nested function call, we restrict the
2059+
// bugdet for the inner function to avoid them being deeply nested.
2060+
// However, when the inner function has a prefix or a suffix
2061+
// (e.g. `foo() as u32`), this budget reduction may produce poorly
2062+
// formatted code, where a prefix or a suffix being left on its own
2063+
// line. Here we explicitlly check those cases.
2064+
if count_newlines(overflowed) == 1 {
2065+
let rw = args.last()
2066+
.and_then(|last_arg| last_arg.rewrite(context, nested_shape));
2067+
let no_newline = rw.as_ref().map_or(false, |s| !s.contains('\n'));
2068+
if no_newline {
2069+
item_vec[args.len() - 1].item = rw;
2070+
} else {
2071+
item_vec[args.len() - 1].item = Some(overflowed.to_owned());
2072+
}
2073+
} else {
2074+
item_vec[args.len() - 1].item = Some(overflowed.to_owned());
2075+
}
2076+
}
20562077
(true, DefinitiveListTactic::Horizontal, placeholder @ Some(..)) => {
20572078
item_vec[args.len() - 1].item = placeholder;
20582079
}
@@ -2824,7 +2845,6 @@ pub fn choose_rhs<R: Rewrite>(
28242845
}
28252846

28262847
fn prefer_next_line(orig_rhs: &str, next_line_rhs: &str) -> bool {
2827-
use utils::count_newlines;
28282848
!next_line_rhs.contains('\n') || count_newlines(orig_rhs) > count_newlines(next_line_rhs) + 1
28292849
}
28302850

src/items.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,8 +1343,9 @@ fn format_tuple_struct(
13431343
// We need to put the where clause on a new line, but we didn't
13441344
// know that earlier, so the where clause will not be indented properly.
13451345
result.push('\n');
1346-
result.push_str(&(offset.block_only() + (context.config.tab_spaces() - 1))
1347-
.to_string(context.config));
1346+
result
1347+
.push_str(&(offset.block_only() + (context.config.tab_spaces() - 1))
1348+
.to_string(context.config));
13481349
}
13491350
result.push_str(&where_clause_str);
13501351

tests/system.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ fn assert_output(source: &Path, expected_filename: &Path) {
164164
if !compare.is_empty() {
165165
let mut failures = HashMap::new();
166166
failures.insert(source.to_owned(), compare);
167-
print_mismatches_default_message(failures, source.display());
167+
print_mismatches_default_message(failures);
168168
assert!(false, "Text does not match expected output");
169169
}
170170
}
@@ -279,7 +279,7 @@ fn check_files(files: Vec<PathBuf>) -> (Vec<FormatReport>, u32, u32) {
279279
Ok(report) => reports.push(report),
280280
Err(err) => {
281281
if let IdempotentCheckError::Mismatch(msg) = err {
282-
print_mismatches_default_message(msg, file_name.display());
282+
print_mismatches_default_message(msg);
283283
}
284284
fails += 1;
285285
}
@@ -291,13 +291,15 @@ fn check_files(files: Vec<PathBuf>) -> (Vec<FormatReport>, u32, u32) {
291291
(reports, count, fails)
292292
}
293293

294-
fn print_mismatches_default_message(
295-
result: HashMap<PathBuf, Vec<Mismatch>>,
296-
file_name: std::path::Display,
297-
) {
298-
print_mismatches(result, |line_num| {
299-
format!("\nMismatch at {}:{}:", file_name, line_num)
300-
});
294+
fn print_mismatches_default_message(result: HashMap<PathBuf, Vec<Mismatch>>) {
295+
let mut t = term::stdout().unwrap();
296+
for (file_name, diff) in result {
297+
let mismatch_msg_formatter =
298+
|line_num| format!("\nMismatch at {}:{}:", file_name.display(), line_num);
299+
print_diff(diff, &mismatch_msg_formatter, Color::Auto);
300+
}
301+
302+
t.reset().unwrap();
301303
}
302304

303305
fn print_mismatches<T: Fn(u32) -> String>(

tests/target/issue-2324.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// nested function calls with cast.
2+
fn main() {
3+
self.ptr
4+
.set(intrinsics::arith_offset(self.ptr.get() as *mut u8, 1) as *mut T);
5+
self.ptr
6+
.set(intrinsics::arith_offset(self.ptr.get(), mem::size_of::<T>() as isize) as *mut u8);
7+
}

0 commit comments

Comments
 (0)