Skip to content

Warn about strings that skip multiple newlines with one backslash #87319

Closed
@joshtriplett

Description

@joshtriplett

(This came up in today's @rust-lang/lang meeting.)

Given the following code:

fn main() {
    let s1 = "a\

b";
    let s2 = "a\

    b";
    println!("{:?} {:/}", s1, s2);
}

Playground link

The compiler interprets both of the string literals as "ab", omitting multiple newlines after the \.

This seems confusing, and different from the behavior of other languages. For instance, C and Python both produce parse errors for such string literals. Or, for a Python multi-line string literal, the backslash escapes the immediately following newline but not the subsequent newline.

Rust does allow unescaped newlines in string literals, which is useful. The ability to escape a newline can also be useful sometimes. But the combination of both escaped and unescaped newlines seems sufficiently confusing and surprising to merit a warning.

We'd ideally like to work towards this being an error. Depending on the presence of existing usage in the ecosystem, that might or might not require an edition.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions