Skip to content

Commit

Permalink
Fix incorrect string indentation in macro defs with format_strings
Browse files Browse the repository at this point in the history
  • Loading branch information
yodaldevoid committed Jan 29, 2022
1 parent b4a4bf0 commit 497548f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
- 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)
- Handles tuple structs that have explicit visibilities and start with a block style comment [#5011](https://github.com/rust-lang/rustfmt/issues/5011)
- Handles leading line-style comments in certain types of macro calls [#4615](https://github.com/rust-lang/rustfmt/issues/4615)
- bizarre string indentation in macro defs with `format_strings` [#4036](https://github.com/rust-lang/rustfmt/issues/4036)


### Added
Expand Down
10 changes: 8 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,14 @@ pub(crate) fn trim_left_preserve_layout(

/// Based on the given line, determine if the next line can be indented or not.
/// This allows to preserve the indentation of multi-line literals.
pub(crate) fn indent_next_line(kind: FullCodeCharKind, _line: &str, config: &Config) -> bool {
!(kind.is_string() || (config.version() == Version::Two && kind.is_commented_string()))
///
/// We indicate that multi-line literals need indentation when `format_strings =
/// true` as the string will only be indented with respect to the formatted code
/// block after formatting. To end with correct indentation, additional
/// indentation must be added to the wrapped portion of the multi-line string.
pub(crate) fn indent_next_line(kind: FullCodeCharKind, line: &str, config: &Config) -> bool {
!((kind.is_string() && !(config.format_strings() && line.ends_with('\\')))
|| (config.version() == Version::Two && kind.is_commented_string()))
}

pub(crate) fn is_empty_line(s: &str) -> bool {
Expand Down
11 changes: 11 additions & 0 deletions tests/source/issue-4036.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// rustfmt-format_strings: true

macro_rules! test {
() => {
fn from() {
None.expect(
"We asserted that `buffer.len()` is exactly `$n` so we can expect `ApInt::from_iter` to be successful.",
)
}
};
}
12 changes: 12 additions & 0 deletions tests/target/issue-4036.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// rustfmt-format_strings: true

macro_rules! test {
() => {
fn from() {
None.expect(
"We asserted that `buffer.len()` is exactly `$n` so we can expect \
`ApInt::from_iter` to be successful.",
)
}
};
}

0 comments on commit 497548f

Please sign in to comment.