Stop Rustfmt adding line breaks

I am trying to stop rustfrm from adding line breaks where they break the visual flow of my code.

I have a piece of code like this which is essentially a 3x4 matrix in a pattern match.

               
                    [_, Term::OWL(VOWL::AssertionProperty), pr],          //:
                    [_, Term::OWL(VOWL::SourceIndividual), Term::Iri(i)], //:
                    [_, target_type, target],                             //:
                    [_, Term::RDF(VRDF::Type), Term::OWL(VOWL::NegativePropertyAssertion)], //:

I was under the impression that the //: would make rustfmt leave the line alone, but it doesn't.

                    [_, Term::OWL(VOWL::AssertionProperty), pr],          //:
                    [_, Term::OWL(VOWL::SourceIndividual), Term::Iri(i)], //:
                    [_, target_type, target],                             //:
                    [
                        _,
                        Term::RDF(VRDF::Type),
                        Term::OWL(VOWL::NegativePropertyAssertion),
                    ], //:

It breaks up the three pattern for the last element by adding new lines (and adds trailing commas, which I can live with). It does leave the previous lines along, which is strange, as I would have thought it should add a trailing comma there also.

Any thoughts on how to stop this? I could add #[rustfmt::skip] but that is going to need to be done in quite a few places.

Not sure if the setting applies to slice patterns, but there is the array_width configuration option. It defaults to 60% of max_width, so setting array_width to max_width (defaults to 100) might be enough for rustfmt to stop breaking up the slice pattern into multiple lines.

Oh, that is strange. This is the point where I really need the full width of the 100 spaces. I hadn't realised it was less for matrixes

You can also add a #[rustfmt::skip] on the top-level array/matrix expression.

Well it seems to work, although cargo fmt now returns this error even when array_width is set to 99 (less that max_width at 100).

Strange

`array_width` cannot have a value that exceeds `max_width`. `array_width` will be set to the same value as `max_width`

Hmm, have you tried explicitly setting max_width=100 in your config file?

Yeah. It's a genuine bug in rustfmt. It fiddles with max_width when you have macros in your code (which I do).

The warning seems otherwise harmless so I will have to live with it.

2 Likes