Skip to content

Unescaped empty braces in format string should suggest escaping them #156868

Description

@kpreid

Code

fn main() {
    println!(
        "fn main() {\n\
            \n\
        }"
    );
}

Current output

error: 1 positional argument in format string, but no arguments were given
 --> src/main.rs:3:20
  |
3 |           "fn main() {\n\
  |  ____________________^
4 | |             \n\
5 | |         }"
  | |_________^

Desired output

The above, followed by

= note: if you intended to print `{`, you can escape it using `{{`

Rationale and extra context

It’s not necessarily obvious that the problem is unescaped braces, in this situation. The proposed note (which already exists for other cases) should, in my opinion, be included in the error whenever there is any kind of whitespace within the braces, which is unusual in correct code.

Other cases

println!(
        "fn main() {\n\
            println!();\n\
        }"
    );

    println!(
        "fn none() -> Option<()> {\n\
            None\n\
        }"
    );

Rust Version

1.95.0

Anything else?

@rustbot label A-fmt

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-fmtArea: `core::fmt`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions