Skip to content

Commit 3ba8719

Browse files
committed
Don't bail out of formatting code blocks if lines are >100 chars
Today rustfmt bails when trying to handle a code block that, after formatting, has lines >100 chars. The reasoning was that if there were lines longer than 100 chars it wasn't clear if we failed to format or that was the intended result, so to be cautious the handler would bail. However, if formatting the snippet fails the code block formatter will already have jumped out by the time this line-width check is done: https://github.com/rust-lang/rustfmt/blob/ff57cc6293ce450380dbb6da6a1a3656dbae538d/src/formatting/util.rs#L81 So I am not sure that this check is needed, and removing it broke no tests except one designed explicitly for it. NB w.r.t. that unit test, formatting a code like ``` fn main() { let expected = "this_line_is_100_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(x, y, z);"; } ``` would yield no change, so I believe it is also the expected result. Closes #4325
1 parent 27b54e9 commit 3ba8719

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

src/formatting/utils.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -792,12 +792,6 @@ pub(crate) fn format_code_block(code_snippet: &str, config: &Config) -> Option<F
792792
}
793793
let trimmed_line = if !is_indented {
794794
line
795-
} else if line.len() > config.max_width() {
796-
// If there are lines that are larger than max width, we cannot tell
797-
// whether we have succeeded but have some comments or strings that
798-
// are too long, or we have failed to format code block. We will be
799-
// conservative and just return `None` in this case.
800-
return None;
801795
} else if line.len() > indent_str.len() {
802796
// Make sure that the line has leading whitespaces.
803797
if line.starts_with(indent_str.as_ref()) {
@@ -885,13 +879,6 @@ mod test {
885879
assert!(test_format_inner(format_snippet, snippet, expected));
886880
}
887881

888-
#[test]
889-
fn test_format_code_block_fail() {
890-
#[rustfmt::skip]
891-
let code_block = "this_line_is_100_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(x, y, z);";
892-
assert!(format_code_block(code_block, &Config::default()).is_none());
893-
}
894-
895882
#[test]
896883
fn test_format_code_block() {
897884
// simple code block
@@ -942,5 +929,11 @@ false,
942929
)
943930
};";
944931
assert!(test_format_inner(format_code_block, code_block, expected));
932+
933+
#[rustfmt::skip]
934+
let code_block = "this_line_is_100_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(x, y, z);";
935+
#[rustfmt::skip]
936+
let expected = "this_line_is_100_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(x, y, z);";
937+
assert!(test_format_inner(format_code_block, code_block, expected));
945938
}
946939
}

tests/target/issue-4325.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
macro_rules! bad {
2+
() => {
3+
macro_rules! inner {
4+
() => {
5+
// This needs to have a width of over 100 characters to trigger the issue 12345678901
6+
("a", "B")
7+
};
8+
}
9+
};
10+
}

0 commit comments

Comments
 (0)