Skip to content

Strings incorrectly unindented with braced macro #6747

@adamspofford-dfinity

Description

@adamspofford-dfinity

This code:

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

Will get 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.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-triageThis issue or PR needs triaging to determine its status. Remove label once sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions