Skip to content

rustfmt barely does any formatting anymore #4460

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
drguildo opened this issue Oct 9, 2020 · 8 comments
Closed

rustfmt barely does any formatting anymore #4460

drguildo opened this issue Oct 9, 2020 · 8 comments

Comments

@drguildo
Copy link

drguildo commented Oct 9, 2020

Apologies if this is how rustfmt has always been, I don't normally use it directly but rather use rust-analyzer which I think uses rustfmt but may have switched to rustfmt from something else recently.

Anyway... rustfmt used to do a very good and comprehensive job of formatting my code, but since (I assume) the last update the quality of formatting has decreased massively. I can have extremely long lines with spurious, inconsistent whitespace and rustfmt will happily ignore it.

I've done my best at remembering and reproducing the kind of formatting that used to take place.

Input

SubCommand::with_name("commit")         .arg(Arg::with_name("message")          .short("m").long("message")   .takes_value(true).required(true))

Output

SubCommand::with_name("commit")         .arg(Arg::with_name("message")          .short("m").long("message")   .takes_value(true).required(true))

Expected output

SubCommand::with_name("commit")
    .arg(Arg::with_name("message")
        .short("m").long("message")
        .takes_value(true)
        .required(true)
    )

Meta

  • rustfmt version: rustfmt 1.4.20-stable (48f6c32 2020-08-09)
  • From where did you install rustfmt?: rustup
@calebcartwright
Copy link
Member

Do you have any non-default configuration values, particularly related to max_width?

@drguildo
Copy link
Author

drguildo commented Oct 9, 2020

@calebcartwright Nope, I've changed nothing. I verified that I get the same output from running the rustfmt executable directly (in case rust-analyzer was influencing things) and it's exactly the same.

@drguildo
Copy link
Author

drguildo commented Oct 9, 2020

I ran rustfmt --print-config current . in one of my repos and got max_width = 100.

@calebcartwright
Copy link
Member

The snippet you've provided isn't reproducible on the playground (I know that's v1.4.22 but I don't anticipate that making a difference)

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=abfeaa866b1eba17ff27f8ce7d2191b8

Can you provide a complete snippet that reproduces the issue?

@calebcartwright
Copy link
Member

I suspect the odds are pretty high that you are running into the existing issue with chains, though there'd have to be more to your snippet to hit that which is why a complete and reproducible snippet will be beneficial so we can rule out any unknown/separate issues.

@calebcartwright
Copy link
Member

Perfect, thanks @drguildo it is indeed the existing chain issue, in your case caused by the length of the about string arg used for read-tree.

In the short term, if you're able/willing to use nightly then you can keep the code as-is and should be able to set format_strings=true to permit rustfmt to be able to wrap that long string arg to fit within max_width which will allow rustfmt to format the entire chain construct.

If you can't use nightly, then you could alternatively consider creating a local variable (e.g. let read_tree_about = "Replace the contents of the current directory with the tree with the specified OID";) and then using that local variable in place of the current string lit, e.g.

        .subcommand(
            SubCommand::with_name("read-tree").about(read_tree_about).arg(Arg::with_name("tree_oid").required(true)),
        )

And then at some point down the road we'll most likely have some improvements within rustfmt to deal with this sort of max_width-exceeding scenario.

I'm going to close this as a duplicate, feel free to subscribe to the #3863 to track updates on said future enhancements

@drguildo
Copy link
Author

drguildo commented Oct 9, 2020

Thanks for the useful advice and help in identifying the cause.

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

3 participants