Skip to content

If a line is too long after formatting, rustfmt gives up on formatting this line #6288

@ArmoredPony

Description

@ArmoredPony

Here is a &str constant with a very long string literal assigned to it:

const REQUIRED_SYMBOLS: &str = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM`1234567890-=[]\\;',./~!@#$%^&*()_+{}:\"<>?";

The total length of this line is 129 characters. This line is not formatted at all. For example, I can do this ugliness

const   REQUIRED_SYMBOLS   :   &     str   =  "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM`1234567890-=[]\\;',./~!@#$%^&*()_+{}:\"<>?";

and these spaces will not be removed upon formatting. However, if we remove just two characters from the string, the line above gets formatted just fine (notice how it splits into two lines):

const REQUIRED_SYMBOLS: &str =
    "qwertyuiosdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM`1234567890-=[]\\;',./~!@#$%^&*()_+{}:\"<>?";

The second line's length with indentation is exactly 100 characters which is the default max_width parameter value. It seems like rustfmt gives up on formatting such statements if it exceeds the max_width even after splitting it.

It doesn't have to be a constant, it may be a variable and it may have a longer name. Only string literal's length matters.

This test used default rustfmt.toml config. In my config, max_width is 80. With that config enabled I have to delete more symbols for rustfmt to format this line.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    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