rust-lang / rust-lang/rustfmt

Strings incorrectly unindented with braced macro

Open
#6,747 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs-triage
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

This code:

fn main() {
    println! {"
                    line 1 \\
                      line 2 \\
                      line 3 \\
                      line 4
                "};
}

Will get (destructively) formatted to this code:

fn main() {
    println! {"
        line 1 \\
          line 2 \\
          line 3 \\
                      line 4
                "};
}

And then if you run the formatter again, it will unindent even further to this code:

fn main() {
    println! {"
    line 1 \\
    line 2 \\
    line 3 \\
                      line 4
                "};
}

which is its final form.
This will also occur for raw strings spelled with single backslashes, which are equivalent:

fn main() {
    println! {r"
        line 1 \
          line 2 \
          line 3 \
                      line 4
                "};
}

As well as regular strings with single backslashes, which can be moved around but it is incorrect to move line 1 (and correct to move line 4).

This does not occur if the macro is spelled with parentheses instead of braces; nor does it occur without any backslashes.
With parentheses, it will move the string opening down but otherwise leave it unmodified:

fn main() {
    println!(
        r"
                    line 1 \
                      line 2 \
                      line 3 \
                      line 4
                "
    );
}

And then if the parentheses in this position are converted to braces, it will not modify the string. The string modification is exclusive to the opening quote going immediately after the brace without a newline. (Note that the original macro here is indoc!, which is supposed to be invoked exactly this way.)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running rustfmt on the minimal braced-macro examples in the issue, comparing them with the parenthesized, raw-string, and no-backslash cases. Trace the formatting path that handles a string immediately after a macro brace; done means preserving the intended indentation and producing identical output on subsequent formatter runs.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.