rust-lang / rustfmt

Format Rust code
https://rust-lang.github.io/rustfmt/
Apache License 2.0
6.02k stars 889 forks source link

[unstable option] error_on_line_overflow #3391

Open scampi opened 5 years ago

scampi commented 5 years ago

Tracking issue for error_on_line_overflow

nrc commented 3 years ago

I would love to have either a warn_on_line_overflow option or to make error_on_line_overflow an enum with Silent, Error, and Warn options.

calebcartwright commented 3 years ago

I really like that idea. I've been struggling lately with the binary nature of the current option, as it (and the default value) are a common source of folks being unaware that there's portions of their codebase that rustfmt isn't able to format at all, but in plenty of cases it wouldn't be viable to make that a hard error either.

I'm leaning towards the multi-variant approach, perhaps with a different name to avoid any confusion/binary connotations. If we go that route we can soft-deprecate the current error_on_line_overflow with an auto-mapping to the corresponding variant on the new option and print a config warning message.

enterprisey commented 1 year ago

I would be interested in working on this if the maintainers still think it's worth doing. I would do the warn_on_line_overflow thing. However, I'm not sure what the difference is between a warning and an error in this context - that is, what the difference between the Error and Warn behaviors would be. I poked around the repo some, and I can't spot any difference - indeed, I can't find the term "warn" even being used that much, besides the warnings about using old config parameter names. Would it be possible to get more details?

calebcartwright commented 1 year ago

I would be interested in working on this if the maintainers still think it's worth doing. I would do the warn_on_line_overflow thing. However, I'm not sure what the difference is between a warning and an error in this context - that is, what the difference between the Error and Warn behaviors would be. I poked around the repo some, and I can't spot any difference - indeed, I can't find the term "warn" even being used that much, besides the warnings about using old config parameter names. Would it be possible to get more details?

That's great, thanks for the offer this is absolutely something we still (desperately) need!

As far as implementation goes, I definitely want to go the single option with multiple variant routes; I think the config surface and expected behavior would be too clunky if we had two separate options that essentially control the "level" of the same thing (e.g. if both are set with true what exactly does the user expect/what should rustfmt do?).

At a high level the work will involve:

https://github.com/rust-lang/rustfmt/blob/a44c7ea5923caa8f908ae0fdd6563033a7ad88da/src/formatting.rs#L598-L615

Veetaha commented 10 months ago

I would like to bring up the problem that I have with this option. I already have a large codebase where enabling this option results in literally 30 000 lines of rustfmt errors printed.

This is because developers have been unaware that rustfmt is unable to get their code under the default of 100 max_width. So with the current state of this unstable option it is impossible to use it on our code base.

I would like to propose that there should be a separate option that specifies the margin for line overflow error. For example, the default rustfmt tries to get all lines under 100 width, but if it can't then there should be, for example a line_overflow_margin = 120 that would not trigger an error_on_line_overflow if the overflow is within the range 100..=120, but if it is 121+ of width, then the error will be triggered.

This would allow us to set a higher margin for line overflow error to reduce the 30 000 lines of rustfmt errors to something manageable in our codebase and work from there incrementally reducing the margin and fixing our code to get a lower margin.

Lorak-mmk commented 3 weeks ago

Documentation for this option says: "Error if Rustfmt is unable to get all lines within max_width, except for comments and string literals. If this happens, then it is a bug in Rustfmt."

Is it really? I tried running cargo fmt -- --config error_on_line_overflow=true --config format_strings=true to workaround https://github.com/rust-lang/rustfmt/issues/3863 (because --config flag for some reason allows unstable options) but it produced a lot of errors on the lines that do not contain comments / string literals. Is this expected or a bug?

To reproduce: checkout https://github.com/scylladb/scylla-rust-driver and run cargo fmt -- --config error_on_line_overflow=true --config format_strings=true.