Skip to content

Commit 84c2356

Browse files
scampitopecongiro
authored andcommitted
handle unicode chars in closures (rust-lang#3632)
The `NotUnicode` branch was unecessarily put on a new line, although it was within max width: ```diff fn baz() { let our_error_b = result_b_from_func.or_else(|e| match e { NotPresent => Err(e).chain_err(|| "env var wasn't provided"), - NotUnicode(_) => Err(e).chain_err(|| "env var was very very very bork文字化ã"), + NotUnicode(_) => { + Err(e).chain_err(|| "env var was very very very bork文字化ã") + } }); } ```
1 parent 944fc57 commit 84c2356

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

src/expr.rs

+9
Original file line numberDiff line numberDiff line change
@@ -2028,6 +2028,15 @@ fn shape_from_rhs_tactic(
20282028
}
20292029
}
20302030

2031+
/// Returns true if formatting next_line_rhs is better on a new line when compared to the
2032+
/// original's line formatting.
2033+
///
2034+
/// It is considered better if:
2035+
/// 1. the tactic is ForceNextLineWithoutIndent
2036+
/// 2. next_line_rhs doesn't have newlines
2037+
/// 3. the original line has more newlines than next_line_rhs
2038+
/// 4. the original formatting of the first line ends with `(`, `{`, or `[` and next_line_rhs
2039+
/// doesn't
20312040
pub(crate) fn prefer_next_line(
20322041
orig_rhs: &str,
20332042
next_line_rhs: &str,

src/lists.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use crate::config::lists::*;
1010
use crate::config::{Config, IndentStyle};
1111
use crate::rewrite::RewriteContext;
1212
use crate::shape::{Indent, Shape};
13-
use crate::utils::{count_newlines, first_line_width, last_line_width, mk_sp, starts_with_newline};
13+
use crate::utils::{
14+
count_newlines, first_line_width, last_line_width, mk_sp, starts_with_newline,
15+
unicode_str_width,
16+
};
1417
use crate::visitor::SnippetProvider;
1518

1619
pub(crate) struct ListFormatting<'a> {
@@ -386,7 +389,7 @@ where
386389
result.push('\n');
387390
result.push_str(indent_str);
388391
// This is the width of the item (without comments).
389-
line_len = item.item.as_ref().map_or(0, String::len);
392+
line_len = item.item.as_ref().map_or(0, |s| unicode_str_width(&s));
390393
}
391394
} else {
392395
result.push(' ');
@@ -808,7 +811,7 @@ where
808811
pub(crate) fn total_item_width(item: &ListItem) -> usize {
809812
comment_len(item.pre_comment.as_ref().map(|x| &(*x)[..]))
810813
+ comment_len(item.post_comment.as_ref().map(|x| &(*x)[..]))
811-
+ item.item.as_ref().map_or(0, String::len)
814+
+ &item.item.as_ref().map_or(0, |s| unicode_str_width(&s))
812815
}
813816

814817
fn comment_len(comment: Option<&str>) -> usize {

src/matches.rs

+3
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,9 @@ fn rewrite_match_body(
468468
next_line_body_shape.width,
469469
);
470470
match (orig_body, next_line_body) {
471+
(Some(ref orig_str), Some(ref next_line_str)) if orig_str == next_line_str => {
472+
combine_orig_body(orig_str)
473+
}
471474
(Some(ref orig_str), Some(ref next_line_str))
472475
if prefer_next_line(orig_str, next_line_str, RhsTactics::Default) =>
473476
{

tests/source/unicode.rs

+8
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,11 @@ pub fn bar(config: &Config) {
2323
csv.write_record(None::<&[u8]>).unwrap();
2424
}
2525
}
26+
27+
// The NotUnicode line is below 100 wrt chars but over it wrt String::len
28+
fn baz() {
29+
let our_error_b = result_b_from_func.or_else(|e| match e {
30+
NotPresent => Err(e).chain_err(|| "env var wasn't provided"),
31+
NotUnicode(_) => Err(e).chain_err(|| "env var was very very very bork文字化ã"),
32+
});
33+
}

tests/target/unicode.rs

+8
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,11 @@ pub fn bar(config: &Config) {
2020
csv.write_record(None::<&[u8]>).unwrap();
2121
}
2222
}
23+
24+
// The NotUnicode line is below 100 wrt chars but over it wrt String::len
25+
fn baz() {
26+
let our_error_b = result_b_from_func.or_else(|e| match e {
27+
NotPresent => Err(e).chain_err(|| "env var wasn't provided"),
28+
NotUnicode(_) => Err(e).chain_err(|| "env var was very very very bork文字化ã"),
29+
});
30+
}

0 commit comments

Comments
 (0)