Skip to content

Commit 59ea71a

Browse files
committed
Fix incorrect string indentation in macro defs with format_strings
1 parent b4a4bf0 commit 59ea71a

File tree

6 files changed

+67
-3
lines changed

6 files changed

+67
-3
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
- Handles cases where certain control flow type expressions have comments between patterns/keywords and the pattern ident contains the keyword [#5009](https://github.com/rust-lang/rustfmt/issues/5009)
4141
- Handles tuple structs that have explicit visibilities and start with a block style comment [#5011](https://github.com/rust-lang/rustfmt/issues/5011)
4242
- Handles leading line-style comments in certain types of macro calls [#4615](https://github.com/rust-lang/rustfmt/issues/4615)
43+
- Fixes bizarre string indentation in macro defs with `format_strings` [#4036](https://github.com/rust-lang/rustfmt/issues/4036)
4344

4445

4546
### Added

Diff for: src/utils.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -646,9 +646,22 @@ pub(crate) fn trim_left_preserve_layout(
646646
}
647647

648648
/// Based on the given line, determine if the next line can be indented or not.
649-
/// This allows to preserve the indentation of multi-line literals.
650-
pub(crate) fn indent_next_line(kind: FullCodeCharKind, _line: &str, config: &Config) -> bool {
651-
!(kind.is_string() || (config.version() == Version::Two && kind.is_commented_string()))
649+
/// This allows to preserve the indentation of multi-line literals when
650+
/// re-inserted a code block that has been formatted separately from the rest
651+
/// of the code, such as code in macro defs or code blocks doc comments.
652+
pub(crate) fn indent_next_line(kind: FullCodeCharKind, line: &str, config: &Config) -> bool {
653+
if kind.is_string() {
654+
// If the string ends with '\', the string has been wrapped over
655+
// multiple lines. If `format_strings = true`, then the indentation of
656+
// strings wrapped over multiple lines will have been adjusted while
657+
// formatting the code block, therefore the string's indentation needs
658+
// to be adjusted for the code surrounding the code block.
659+
config.format_strings() && line.ends_with('\\')
660+
} else if config.version() == Version::Two {
661+
!kind.is_commented_string()
662+
} else {
663+
true
664+
}
652665
}
653666

654667
pub(crate) fn is_empty_line(s: &str) -> bool {

Diff for: tests/source/issue-4036-2.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// rustfmt-format_strings: true
2+
3+
macro_rules! test {
4+
() => {
5+
fn from() {
6+
None.expect(
7+
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
8+
)
9+
}
10+
};
11+
}

Diff for: tests/source/issue-4036.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// rustfmt-format_strings: true
2+
3+
macro_rules! test {
4+
() => {
5+
fn from() {
6+
None.expect(
7+
"We asserted that `buffer.len()` is exactly `$n` so we can expect `ApInt::from_iter` to be successful.",
8+
)
9+
}
10+
};
11+
}

Diff for: tests/target/issue-4036-2.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// rustfmt-format_strings: true
2+
3+
macro_rules! test {
4+
() => {
5+
fn from() {
6+
None.expect(
7+
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor \
8+
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis \
9+
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. \
10+
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu \
11+
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in \
12+
culpa qui officia deserunt mollit anim id est laborum.",
13+
)
14+
}
15+
};
16+
}

Diff for: tests/target/issue-4036.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// rustfmt-format_strings: true
2+
3+
macro_rules! test {
4+
() => {
5+
fn from() {
6+
None.expect(
7+
"We asserted that `buffer.len()` is exactly `$n` so we can expect \
8+
`ApInt::from_iter` to be successful.",
9+
)
10+
}
11+
};
12+
}

0 commit comments

Comments
 (0)