Skip to content

rewriting chains fails if one line fails to wrap #5334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
JakeHillion opened this issue May 6, 2022 · 3 comments
Closed

rewriting chains fails if one line fails to wrap #5334

JakeHillion opened this issue May 6, 2022 · 3 comments

Comments

@JakeHillion
Copy link

Consider the following example (using clap, as it's one of the easiest builders to demonstrate with):

use clap::{Arg, Command};

fn main() {
    let matches = Command::new("app")
        .arg(Arg::new("debug").long("debug").short('d').help("Enable additional debugging.").takes_value(false))
        .about("This string is really super long and rustfmt refuses to wrap it, so long that it isn't even suitable on a line of its own.")
        .arg(Arg::new("debug").long("debug").short('d').help("Enable additional debugging.").takes_value(false))
        .get_matches();
}

rustfmt does nothing (tested on current playground, 1.4.38-nightly (2022-05-05 30f3860)).

Whereas the following example, without the exceedingly long string:

use clap::{Arg, Command};

fn main() {
    let matches = Command::new("app")
        .arg(Arg::new("debug").long("debug").short('d').help("Enable additional debugging.").takes_value(false))
        .about("This is a short string.")
        .arg(Arg::new("debug").long("debug").short('d').help("Enable additional debugging.").takes_value(false))
        .get_matches();
}

Formats to the following (what I would expect):

use clap::{Arg, Command};

fn main() {
    let matches = Command::new("app")
        .arg(
            Arg::new("debug")
                .long("debug")
                .short('d')
                .help("Enable additional debugging.")
                .takes_value(false),
        )
        .about("This is a short string.")
        .arg(
            Arg::new("debug")
                .long("debug")
                .short('d')
                .help("Enable additional debugging.")
                .takes_value(false),
        )
        .get_matches();
}

It seems like rustfmt gives up on the entire statement if any part won't satisfy the width heuristic in the output, rather than still doing its best. The ideal formatting for the long string version would be the final output of the short string version, with the short string substituted. With extremely long builder chains this leads to horrible formatting, as it must all be done manually.

@ytmimi
Copy link
Contributor

ytmimi commented May 6, 2022

Thanks for the report!

This is a known issue and is a duplicate of #3416, and #3863

@ytmimi ytmimi closed this as completed May 6, 2022
@ytmimi
Copy link
Contributor

ytmimi commented May 6, 2022

If you can set format_strings=true, then rustfmt will be able to wrap the long string and the code will be reformatted.

@JakeHillion
Copy link
Author

Thanks for that! I'd read the flagged issues and it's a pretty good solution, though does involve switching to nightly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants